The Different Ways to Style a Schedule
In this blog post we will look at the different levels of sty‌ling the elements and items of a schedule made with a MindFusion scheduling library. In our sample we use the Java Scheduling Library, but the API members that we use and the cascading levels of styling a schedule are universal across all MindFusion Scheduling components.
I. Top Level: Theme
At the top level of styling you have themes. The scheduling library has a set of predefined themes: Light, Lila, Silver, Standard, Vista, Windows2013. You apply one of the predefined themes this way:
calendar.setTheme(ThemeType.Vista);
Here is what the calendar looks like styled with the vista theme:
In JavaScript, the themes are defined in CSS styles and you must reference the desired file and set the name of the theme to the calendar instance. In the other libraries, they are built-in and usually are available as members of an enum.
You have the option to create a custom theme, which you can save and apply this way:
calendar.setCustomTheme(new MyTheme());
Another way to apply the custom theme in Java Scheduling is:
calendar.setCustomTheme(new MyTheme());
Here is the look of the calendar with the custom theme:
The custom theme has changed some of the colors, widened the header so that the week days could be seen and set some borders on the cells.
Creating a custom Theme requires that you override a strict set of methods. They are the public members of the Theme class. You should override all of them. Among the methods that style the calendar for each view – getMonthRangeSettings getMonthSettings etc. You could override in detail only the method that is responsible for styling the view you would use – if it is only one. For the rest of the methods you could just write:
private void initialize_MyTheme_ListViewSettings() { _listViewSettings = super.createListViewSettings(); }
Every method in the Theme class must be dutifully implemented and all settings set. That comes from the fact that a Theme is the last and topmost styling instrument and it must know how to style any element that might not be explicitly styled down the tree.
The online and offline documentations of the Java Scheduling library come with topics that list in details the styling settings for each of the predefined themes. Our advice is that you get the code of the Theme that looks closest to what you want to have as a structure and modify it.
The sample project that you can download at the bottom of this post implements a custom Theme based on the Vista theme and lists all members in a theme that you must set with all details.
II. View and Item Settings
One level down the tree are the view settings properties. They are available for any view. You can access those settings with the getMonthSettings getMonthRangeSettings etc. methods. Each one of those methods returns the styling settings of a particular view. You should use the one that corresponds to the view you’ve chosen:
//set the view to SingleMonth calendar.setCurrentView(CalendarView.SingleMonth); //get the styling settings for SingleMonth view calendar.getMonthSettings().getDaySettings().setTodayFillColor(Color.green);
You can style the items, regardless of the view used, with the ItemSettings object:
calendar.getItemSettings().setPadding(20);
The *Settings properties define the appearance of items in terms of alignment, spacing, padding, shadow, date format. The coloring of the elements is left to Style instances. Thus, if you want to change the color of items, you will use:
//customize just the items through the itemSettings field calendar.getItemSettings().setPadding(20); Style itemStyle = new Style(); itemStyle.setBrush(new SolidBrush(Color.white)); itemStyle.setHeaderTextColor(Color.DARK_GRAY); itemStyle.setHeaderTextShadowStyle(ShadowStyle.None); calendar.getItemSettings().setStyle(itemStyle);
This styles all items on the calendar. For styling a particular item, you should use on of the methods listed underneath.
Our calendar now has green header on the current day, the background of events is white and there is a bit of a padding added to the events.
III. Using Events to Style Particular Items
When you want to select items that you want to style based on some distinct characteristics, you can use events. In our sample we handle the itemCreated event, where we check if the start date of an appointment happens to be during the weekend:
// Listen for item creation and for draw events calendar.addCalendarListener(new CalendarAdapter(){ //apply custom styling to selected items public void itemCreated(ItemEvent e) { onItemCreated(e); } });
The Java Scheduler provides various events, which are accessible through a CalendarListener and CalendarAdapter instances. We handle the itemCreated event this way:
//color in red events that are scheduled to start on weekends protected void onItemCreated(ItemEvent e) { Item item = e.getItem(); if(item.getStartTime().getDayOfWeek() == DayOfWeek.Saturday || item.getStartTime().getDayOfWeek() == DayOfWeek.Sunday) { item.getStyle().setBrush(new SolidBrush(new Color(213, 28, 32))); item.getStyle().setHeaderTextColor(Colors.White); item.getPointedStyle().setBrush(new SolidBrush(new Color(100, 100, 100))); } }
The ItemEvent class provides the item that was created and you can use the instance to apply any particular styling to the item.
Here is our scheduler, which now colors the items on weekends in red:
In JavaScript, the items have a special field that allows you to assign to them a custom CSS style that you’ve defined. The style will be applied to the particular item only. The field is called ‘cssClass’.
IV. Custom Drawing
When you need to style in a very unique way calendar elements and nothing else helps, you have the option to draw them the way you want. Custom drawing can be made for many parts of the calendar. The available elements are identified as members of the CustomDrawElements enumeration.
You tell the control that you want to use custom drawing this way:
//specify that we will use custom drawing calendar.setCustomDraw(EnumSet.of(CustomDrawElements.CellContents));
The custom drawing must be part of the draw method, which is also a member of CalendarListener:
// Listen for item creation and for draw events calendar.addCalendarListener(new CalendarAdapter(){ //add custom drawing to CellContents @Override() public void draw(CalendarDrawEvent e) { onDraw(e); } });
The event handler method looks like this:
//apply custom drawing to selected items private void onDraw(CalendarDrawEvent e) { if (e.getElement() == CustomDrawElements.CellContents) { DateTime date = e.getDate(); //color in light yellow the background of the first 10 days of a month if (date.getDay() < 11) { // Do the custom drawing Rectangle2D bounds = new Rectangle2D.Double( e.getBounds().getX(), e.getBounds().getY(), e.getBounds().getWidth() - 1, e.getBounds().getHeight() - 1); new AwtGraphics(e.getGraphics()).fillRectangle(Brushes.LightYellow, bounds); } } }
The Calendar’s drawEvent class gives us useful methods to learn more about the item that is being drawn. In our case we want to draw the cell contents, so we check if draw was called for the cell contents, and if yes, we get the bounds of the element. We need the check, because draw is called for all elements that support custom drawing and we need to identify which one is drawn at the moment.
Thanks to the custom drawing, the monthly schedule now has a light yellow background on the first ten days of the month:
With this our review of the methods to style a schedule is finished. You can download the complete source code of the sample together with all MindFusion libraries used from this link:
How to Style a Java Schedule: Download Project Source Code
You can post questions about Mindusion Scheduling components at MindFusion online forums.
About MindFusion Scheduling Components MindFusion Scheduling components are available for a variety of platforms for web, mobile and desktop programming. All of them include a robust feature set that includes 6 calendar views, predefined themes, various events, predefined forms for creating appointments and recurrence. The components are fully interactive, easy to customize and style and are the ideal choice for any application that needs to implement time management features. You can learn more about MindFusion Scheduling tools at https://mindfusion.eu/scheduling-pack.html.
Cost Meter Gauge in JavaScript
In this post we will look at the steps we need to make if we want to create this beautiful gauge below:
The gauge is done with MindFusion Charts and Gauges for JavaScript library. You can download the sample together with all needed libraries from this link.
I. Project Setup
We will build the gauge using the OvalGauge library from MindFusion JS Charts and Gauges control. We add two references, needed for the control to work properly:
<script type="text/javascript" src="Scripts/MindFusion.Common.js"></script> <script type="text/javascript" src="Scripts/MindFusion.Gauges.js"></script>
We have placed those files in a Scripts folder. We will write the JavaScript code for the gauge in a separate file, which we call ValueGauge.js. This file is at the same directory where the web page is. We add a reference to it as well:
<script type="text/javascript" src="ValueGauge.js"></script>
The web page with our sample contains a table. We use the table to place the control together with a text box. The text box is not needed, but we will use it to give the user the option to set the value of the gauge by typing it not only by dragging the pointer.
<table cellpadding="10"> <tbody> <tr> <td>Project Cost (in thousands)</td> </tr> <tr> <td><canvas id="value_meter" width="300" height="300"></canvas></td> </tr> <tr> <td>Cost <input id="cost" style="width: 80px"></td> </tr> </tbody> </table>
The gauge will be rendered through an HTML Canvas element. The location and size of the Canvas determine the location and the size of the gauge. It is important that we add an id the Canvas – this way we can reference it in the JavaScript code page, which will be necessary.
II. The Control
Now we start editing the ValueGauge.js file and there we first add mappings to the namespaces of Mindusion.Gauges.js that we will use:
/// var Gauges = MindFusion.Gauges; var d = MindFusion.Drawing; var OvalScale = Gauges.OvalScale; var Length = Gauges.Length; var LengthType = Gauges.LengthType; var Thickness = Gauges.Thickness; var Alignment = Gauges.Alignment; var LabelRotation = Gauges.LabelRotation; var TickShape = Gauges.TickShape; var PointerShape = Gauges.PointerShape;
The first line is a reference to the Intellisense file that allows us to use code completion of the API members, if supported by our JavaScript IDE.
Now we need to get the DOM Element that corresponds to the gauge Canvas and use it to create an instance of the OvalGauge class:
var value_meter = Gauges.OvalGauge.create(document.getElementById('value_meter'), false);
III. Gauge Scales
Gauge scales depend on the type of the gauge. For oval gauges we use OvalScale The OvalScale needs to be associated with a gauge and here is how we create it:
var valueScale = new Gauges.OvalScale(value_meter); valueScale.setMinValue(0); valueScale.setMaxValue(100); valueScale.setStartAngle(120); valueScale.setEndAngle(420);
The OvalScale class offers the full set of properties needed to customize the scale. We use the setMinValue and setMaxValue methods to specify the value range o the gauge. The setStartAngle and setEndAngle specify the arc of the gauge and we set them to 120 and 420 respectively. You see that the arc is 300 degrees, which is less than a full circle – exactly how we want it to be.
We continue our customization by setting the fill and stroke of the scale. We actually do not want the default scale to be rendered at all, so we use setFill and setStroke to specify transparent colors:
valueScale.setFill('Transparent'); valueScale.setStroke('Transparent'); valueScale.setMargin(new Gauges.Thickness(0.075, 0.075, 0.075, 0.075, true));
Now we can continue with the ticks. Each gauge can have major, middle and minor ticks. Those ticks are not rendered by default.
var majorSettings = valueScale.majorTickSettings; majorSettings.setTickShape(Gauges.TickShape.Line); majorSettings.setTickWidth(new Gauges.Length(10, Gauges.LengthType.Relative)); majorSettings.setTickHeight(new Gauges.Length(10, Gauges.LengthType.Relative)); majorSettings.setFontSize(new Length(14, LengthType.Relative)); majorSettings.setNumberPrecision(0); majorSettings.setFill('rgb(46, 52, 66)'); majorSettings.setStroke('rgb(46, 52, 66)'); majorSettings.setLabelForeground('rgb(175, 175, 175)'); majorSettings.setLabelAlignment(Alignment.InnerCenter); majorSettings.setLabelRotation(LabelRotation.Auto); majorSettings.setLabelOffset(new Length(6, LengthType.Relative)); majorSettings.setStep(20); majorSettings.setTickAlignment (Alignment.OuterOutside);
We start the customization with the majorTickSettings They will render labels and want to have one tick with a tep of 20. So, we use setStep to specify 20 as an interval and use setTickWidth and setTickHeight to set the size of the ticks. Those properties can be set to an absolute or relative value – see the LengthType enumeration. We also need to change the default shape of the pointer – we use TickShape rest of the settings are intuitive – setFill and setStroke specify how the ticks are colored. We also use setLabelAlignment to position the labels outside the ticks. setTickAlignment is also an important property -it allows us to change the alignment of the ticks, so they can be drawn inside the scale.
The TickSettings object is similar to MajorTickSettings
var middleSettings = valueScale.middleTickSettings; middleSettings.setTickShape(TickShape.Line); middleSettings.setTickWidth(new Gauges.Length(10, Gauges.LengthType.Relative)); middleSettings.setTickHeight(new Gauges.Length(10, Gauges.LengthType.Relative)); middleSettings.setTickAlignment (Alignment.OuterOutside); middleSettings.setShowTicks(true); middleSettings.setShowLabels(false); middleSettings.setFill('rgb(46, 52, 66)'); middleSettings.setStroke('rgb(46, 52, 66)'); middleSettings.setCount(5);
We should note here that setShowLabels is false because we want the labels to appear only at intervals of 20. We also use setCount to specify how many ticks we want between each two adjacent major ticks. The rest of the settings are the same as for MajorTickSettings.
IV. Custom Painting
The painting of the colored sections at the edge of the gauge is custom code. The gauges library provides various events that allow the developer to replace the default gauge drawing with custom drawing – see the Events section of the OvalGauge class.
In our sample we will handle two events:
value_meter.addEventListener(Gauges.Events.prepaintBackground, onPrepaintBackground.bind(this)); value_meter.addEventListener(Gauges.Events.prepaintForeground, onPrepaintForeground.bind(this));
prepaintBackground is raised before the background is painted. We can cancel the default painting or add some custom drawing to it. The same is true for prepaintForeground
function onPrepaintBackground(sender, args) { args.setCancelDefaultPainting(true); var context = args.getContext(); var element = args.getElement(); var bounds = new d.Rect(0, 0, element.getRenderSize().width, element.getRenderSize().height); .................................. }
In the prepaintBackground event handler we first get the handler to the CanvasRenderingContext2D instance. Then we get the bounds of the painted element. This is the inside of the gauge. Each o the colored segments is pained as an arc. We do not create a path figure to fill – instead we set a very thick lineWidth of the stroke:
context.lineWidth = 25; var correction = context.lineWidth / 2; //light green segment context.beginPath(); context.strokeStyle = 'rgb(0, 205, 154)'; context.arc(bounds.center().x, bounds.center().y, bounds.width / 2-correction, 0.665*Math.PI, 1*Math.PI, false); context.stroke();
We go on painting this way all colored sections of the gauge, only changing the start and end angles. When we are ready we paint the inside of the gauge. We do it with a full arc:
context.beginPath(); bounds.x += 25; bounds.y += 25; bounds.width -= 50; bounds.height -= 50; context.fillStyle = '#2e3442'; context.arc(bounds.center().x, bounds.center().y, bounds.width / 2, 0*Math.PI, 2*Math.PI, false); context.fill();
The complete drawing is done inside the prepaintBackground event handler. So, in the prepaintForeground handler we only need to cancel the default painting:
function onPrepaintForeground(sender, args) { args.setCancelDefaultPainting(true); };
V. The Gauge Pointer
We need to add a Pointer to the OvalScale of the gauge instance if we want to show one:
var pointer = new Gauges.Pointer(); pointer.setFill('white'); pointer.setStroke("#333333"); pointer.setPointerWidth(new Gauges.Length(90, Gauges.LengthType.Relative)); pointer.setPointerHeight(new Gauges.Length(20, Gauges.LengthType.Relative)); pointer.setShape(Gauges.PointerShape.Needle2); pointer.setIsInteractive(true); valueScale.addPointer(pointer);
The size of the pointer is also set in LengthType units. This allows us to have the same pointer size relative to the size of the gauge even if we change the size of the Canvas. We use the PointerShape enumeration to specify the type of pointer we want and then we make it interactive with setIsInteractive As an addition to the default needle of the pointer we want to render a circle at the base of the pointer. We do it with custom drawing:
value_meter.addEventListener(Gauges.Events.prepaintPointer, onPrepaintPointer.bind(this));
First we need to handle the prepaintPointer event. In the event handling code we do the drawing:
function onPrepaintPointer(sender, args) { //args.setCancelDefaultPainting(true); var context = args.getContext(); var element = args.getElement(); var size = element.getRenderSize(); var psize = new d.Size(0.2 * size.width, size.height); context.save(); context.transform.apply(context, element.transform.matrix()); context.beginPath(); context.arc(psize.width / 2, psize.height / 2, psize.height*0.75, 0, 2 * Math.PI, false); var fill = element.getFill(); context.fillStyle = Gauges.Utils.getBrush(context, fill, new d.Rect(0, 0, size.width, size.height), false); context.fill(); context.strokeStyle = '#333333'; context.stroke(); context.restore(); };
Note that in this case we do not cancel the default painting – we will add to it, rather than replace it. Then we get the CanvasRenderingContext2D and size of the rendered element. What is new here is the transform of the CanvasRenderingContext2D object to the center of the gauge. Then we get the Brush that is used to paint the rest of the pointer and use it to fill the custom part as well. We can set the brush directly, but we prefer to take it from the base element – the Pointer This way if we change settings of the Pointer the color of the custom drawn circle will change automatically as well.
VI. Data Binding
What we would like to do now is bind a change in the text field to the value of the gauge scale. We add a method that does it:
function valueChanged(id) { if (isNaN(this.value)) return; var gauge = Gauges.OvalGauge.find(id); var pointer = gauge.scales[0].pointers[0]; pointer.setValue(+this.value); };
When we call the valueChanged method with the instance of the OvalGauge as an argument, we can get its pointer and set its value to the value of ‘this’. We call the valueChanged in such way, that the ‘this’ reference will be the text field:
var cost = document.getElementById('cost'); cost.onchange = valueChanged.bind(cost, ['value_meter']);
Now when the value changes, the event handler takes the pointer and set its value to the value the user has types.
That is the end of this tutorial. You can download the source code of the sample, together with all MindFusion libraries used from the following link:
Download Value Gauge in JavaScript Source Code
You can use the discussion forum to post your questions, comments and recommendations about the sample or MindFusion charts and gauges.
About MindFusion JavaScript Gauges: A set of two gauge controls: oval and rectangular, with the option to add unlimited nuber of scales and gauges. All gauge elements support complete customization of their appearance. Custom drawing is also possible, where you can replace the default rendering of the gauge element or add to it. The gauge controls include a variety of samples that offer beautiful implementations of the most popular applications of gauges: thermometer, car dashboard, functions, compass, clock, cost meter and more.
Gauges for JavaScript is part of MindFusion charts and Dashboards for JavaScript. Details at https://mindfusion.eu/javascript-chart.html.
Horizontal Full Bar Chart in JavaScript
We use here MindFusion JavaScript library for Charts and Gauges to build this horizontal stacked bar chart that renders custom tooltips:
Run the sample from this link.
You can download the source code together with the libraries used from the link at the bottom of the post.
I. General Setup
We split our chart in two files – one is the web page that hosts an HTML Canvas element that will render the chart. The other file is a JavaScript code-behind file that contains the code for the chart.
We need to add reference to two JavaScript library files that provide the charting and drawing functionality that we need:
MindFusion.Common.js
MindFusion.Charting.js
We place them in a Scripts folder at the same level as our web page and JavaScript code behind file.
<script type="text/javascript" src="Scripts/MindFusion.Common.js"></script> <script type="text/javascript" src="Scripts/MindFusion.Charting.js"></script>
We also add a reference to the code-behind file that we call StackedBarChart.js:
<script type="text/javascript" src="StackedBarChart.js"></script>
Now we need to create an HTML Canvas element and we must provide it with an id so we can reference it in our JS code:
<canvas id="barChart" width="600px" height="400px"></canvas>
The size of the Canvas determines the size of the chart.
II. Chart Instance and General Settings
We add some namespace mappings that allow us to reference classes from the Chart library in a more consice manner:
var Charting = MindFusion.Charting; var Controls = MindFusion.Charting.Controls; var Collections = MindFusion.Charting.Collections; var Drawing = MindFusion.Charting.Drawing; var GridType = MindFusion.Charting.GridType; var ToolTip = Charting.ToolTip;
Then we create an instance of the BarChart control. We need to get the Dom Element that corresponds to the Canvas that we’ve prepared for the chart:
var chartEl = document.getElementById('barChart'); chartEl.width = chartEl.offsetParent.clientWidth; chartEl.height = chartEl.offsetParent.clientHeight; var chart = new Controls.BarChart(chartEl, Charting.BarLayout.Stack);
The BarChart constructor supports a second argument that indicates the type of the bar chart to render.
We set the bar chart to horizontal with the horizontalBars property. We also make the bars thicker than normal – the property for this is barSpacingRatio It measures the thickness of the bars as a percente of the bar width.
chart.horizontalBars = true; chart.barSpacingRatio = 0.2;
III. The Data Series
We want our chart to render labels as tooltips, inside the bars and also we want custom labels at the Y-axis. The predefined BarSeries class accepts 4 lists with data: one for bar data and three with labels inside the bars, at the top of the bars and at the X-axis. So, it is not an exact match for what we want to do and we need to customize it.
We will create our own custom BarSeries that we will call SeriesWithLabels. We will inherit the BarSeries class and override its constructor and getLabel members to provide the desired data for the desired type of labels.
We override the constructor by creating three new variables, which receive the data for the bars and the labels:
var SeriesWithLabels = function (barValues, innerLabels, yLabels) { Charting.BarSeries.apply(this, [barValues, innerLabels, yLabels]); this.yLabels = yLabels; this.innerLabels = innerLabels; this.values = barValues; }; SeriesWithLabels.prototype = Object.create(Charting.BarSeries.prototype);
Note that before we do anything else in the new constructor we need to call the apply method of the BarSeries class to transfer the provided data to the base class. We also need to create a prototype of the new series and also define its constructor:
Object.defineProperty(SeriesWithLabels.prototype, 'constructor', { value: SeriesWithLabels, enumerable: false, writable: true });
Next we will override the getLabel method. This is the method that returns the correct label according to the requested label kind and the index of the label. We said we want to support inner labels, tooltips and Y-axis labels. So, we make sure our implementation of getLabel returns exactly those labels:
SeriesWithLabels.prototype.getLabel = function (index, kind) { if ((kind & Charting.LabelKinds.YAxisLabel) != 0 && this.yLabels) return this.yLabels.items()[index]; if ((kind & Charting.LabelKinds.InnerLabel) != 0 && this.innerLabels) return this.innerLabels.items()[index]; if ((kind & Charting.LabelKinds.ToolTip) != 0) return getPercentLabel(index, this); return ""; };
Getting the correct inner and top label is easy – we just return the label at the requested position. What is more work is building the tooltip. We want our tooltip to calculate the portion of the part in the stacked bar the mouse currently is over, to the entire bar. This means we need to calculate the data of all bar portions, which is a combination of the values at the requested position in all three bar series. We do this calculation in a separate method called getPercentLabel.
Before we get to the getPercentLabel method let’s create 3 instances of our custom SeriesWithLabels class:
var labels = new Collections.List([ "POSITION", "SALARY", "LOCATION", "COLLEAGUES", "WORKTIME" ]); // create sample data series var series1 = new SeriesWithLabels(new Collections.List([123, 212, 220, 115, 0.01]), new Collections.List([123, 212, 220, 115, 0]), labels); var series2 = new SeriesWithLabels(new Collections.List([53, 132, 42, 105, 80]), new Collections.List([53, 132, 42, 105, 80]), null); var series3 = new SeriesWithLabels(new Collections.List([224, 56, 138, 180, 320]), new Collections.List([224, 56, 138, 180, 320]), null);
The third argument in the SeriesWithLabels constructor is the lists with labels at the Y-axis. We need just one list with labels and we set it with the first series. The other series take null as their third argument.
We need to create a collection with the series and assign it to the series property of the chart:
var series = new Collections.ObservableCollection(new Array(series1, series2, series3)); chart.series = series;
There is a special property called supportedLabels that is member of Series and tells the chart, what type of labels this Series needs to draw. In our case we need to indicate that the first series renders labels at the Y-axis, the inner labels and tooltips. The other two series render inner labels and tooltips:
series1.supportedLabels = Charting.LabelKinds.YAxisLabel | Charting.LabelKinds.InnerLabel | Charting.LabelKinds.ToolTip; series2.supportedLabels = Charting.LabelKinds.InnerLabel | Charting.LabelKinds.ToolTip; series3.supportedLabels = Charting.LabelKinds.InnerLabel | Charting.LabelKinds.ToolTip;
Now let’s get back to the method that calculates the tooltip:
function getPercentLabel(index, series) { var value1 = series1.values.items()[index]; var value2 = series2.values.items()[index]; var value3 = series3.values.items()[index]; var theValue = series.values.items()[index]; var result = theValue/(value1+value2+value3) * 100; return Number(result).toFixed(0) + "%"; };
In it we calculate the sum of all data that is rendered by the stacked bar at the specified index. Then we convert the data to percent and format it to have no numbers after the decimal point. That gives us a little inacurracy sometimes, when the value gets rounded to the next number and the sum of all percents actually is 101. You might want to change the formatting to toFixed(2) if you want to see the exact number rendered.
IV. Axes and Tooltip
By default the X-axis shows a title and both axes render the auto scale for the data of the chart. We need to hide the scale and we also hide the ticks that are rendered at the interval values:
chart.xAxis.title = ""; chart.yAxis.title = ""; chart.showXCoordinates = false; chart.showYCoordinates = false; chart.showXTicks = false; chart.showYTicks = false;
We don’t want our chart to render axes at all, so we will draw them with the color of the chart background. You can also draw them with a transparent brush:
chart.theme.axisStroke = new Drawing.Brush(Drawing.Color.knownColors.White);
The tooltip renders automatically when the user hovers a bar. We can customize it with the properties of the static Tooltip class:
ToolTip.brush = new Drawing.Brush("#fafafa"); ToolTip.pen = new Drawing.Pen("#9caac6"); ToolTip.textBrush = new Drawing.Brush("#5050c0"); ToolTip.horizontalPadding = 6; ToolTip.verticalPadding = 4; ToolTip.horizontalOffset = 76; ToolTip.verticalOffset = 34; ToolTip.font = new Charting.Drawing.Font("Verdana", 12);
We add some padding to the tooltip text and increase its font size. We also render the tooltip with a little offset that will place it inside the bar, ater the inner label.
V. Styling and Legend
Styling o the charts is done through instances of SeriesStyle derived classes. The instance is assigned to the seriesStyle property of the Chart In our case we want to color each bar in three sections. That means the portion of the bar that corresponds to the same series is colored in the same color for all its members. That kind of styling is supported by the PerSeriesStyle class. It accepts a list with brushes and strokes and paints all elements of the series corresponding to the index of the brush in the list with this brush:
// create bar brushes var thirdBrush = new Drawing.Brush("#97b5b5"); var secondBrush = new Drawing.Brush("#5a79a5"); var firstBrush = new Drawing.Brush("#003466"); // assign one brush per series var brushes = new Collections.List([firstBrush, secondBrush, thirdBrush]); chart.plot.seriesStyle = new Charting.PerSeriesStyle(brushes, brushes);
The theme property is the main property for styling the chart. The Theme class exposes fields for customizing the appearance of all chart elements. We first adjust the font and size of the axis labels – remember we have labels only at the Y-axis:
chart.theme.axisTitleFontSize = 14; chart.theme.axisLabelsFontSize = 11; chart.theme.axisTitleFontName = "Verdana"; chart.theme.axisLabelsFontName = "Verdana"; chart.theme.axisLabelsFontSize = 14; chart.theme.axisStroke = new Drawing.Brush(Drawing.Color.knownColors.White);
The labels inside the bars are called data labels and there are dataLabels*** properties that regulate their appearance:
chart.theme.dataLabelsFontName = "Verdana"; chart.theme.dataLabelsFontSize = 14; chart.theme.dataLabelsBrush = new Drawing.Brush("#ffffff");
The dataLabelsBrush is also used when the legend labels are rendered. In order to make them visible we need to set a darker background for the legend:
chart.theme.legendBackground = new Drawing.Brush("#cccccc"); chart.theme.legendBorderStroke = new Drawing.Brush("#cecece");
The labels inside the legend are taken from the title property of the Series instances:
series.item(0).title = "CAREER START"; series.item(1).title = "MIDDLE OF CAREER"; series.item(2).title = "CAREER END";
Finally we should not forget to call the draw method that actually renders the chart:
chart.draw();
With this our costimization of the chart is done. You can download the source code of the sample and the MindFusion JavaScript libraries used from this link:
Download the Horizontal Stacked Bar Chart Sample: Source Code and Libraries
About Charting for JavaScript: MindFusion library for interactive charts and gauges. It supports all common chart types including 3D bar charts. Charts can have a grid, a legend, unlimitd number of axes and series. Scroll, zoom and pan are supported out of the box. You can easily create your own chart series by implementing the Series interface.
The gauges library is part of Charting for JavaScript. It supports oval and linear gauge with several types of labels and ticks. Various samples show you how the implement the gauges to create and customize all popular gauge types: car dashboard, clock, thermometer, compass etc. Learn more about Charting and Gauges for JavaScript at https://mindfusion.eu/javascript-chart.html.
Pie Chart with Custom Labels in WinForms
In this tutorial we will build the following pie chart using MindFusion Charts and Gauges for WinForms library:
This is a PieChart with a custom Series class that extends the capabilities of a standard PieSeries We need to create a custom Series class because we want the inner labels to follow a special format and we want the outer labels to be rendered as legend items not near pie pieces.
You can download the source code of the sample together with the MindFusion libraries used from the link at the bottom of the post.
I. General Setup
We create an empty WinForms application in C# with Visual Studio. We create a folder called References and there we copy the MindFusion.*.dll -s that we will need. They are:
MindFusion.Charting.dll
MindFusion.Charting.WinForms.dll
MindFusion.Common.dll
We reference those files in our project: we right-click the References folder in the Solution Explorer and click on “Add Reference”. Then we browse to our local References folder and add the 3 dll-s.
We have installed MindFusion Charts and Gauges from the website: http://mindfusion.eu/ChartWinFormsTrial.zip and now we need only to find the PieChart control, drag and drop it onto the WinForms Form of our application.
II. The Custom Pie Series
When you create a custom series you need to implement the Series interface. You need to declare several methods and properties, and one event. Let’s start with the constructor.
We need our chart to use one array with data and one array with labels. We declare two class variables for that and assign to them the values that we receive for them in the constructor. We name the new class CustomPieSeries:
public class CustomPieSeries : Series { public CustomPieSeries(IList data, IList legendLabels ) { values = data; _legendLabels = legendLabels; //sum up all pie data total = 0.0; for (int i = 0; i < data.Count; i++) total += data[i]; } IList values; IList _legendLabels; double total = 0L; }
We have added a new class variable called total. It is needed to keep the sum of all data for the chart. We will use this variable when we calculate the percent value of each pie piece.
The SupportedLabels property of type LabelKinds is the way to set which labels will be rendered by the new series. We want tooltips, inner labels and legend labels. There is no special enum field for legend labels. You just set which labels form the series should be rendered as legend items. We decide to use for this the ZAxisLabel, because it is not drawn anywhere on the pie chart and we won’t see it doubled by the legend labels. So, we say that the chart supports ZAxisLabels and we will tell the series that the ZAxisLabels must be rendered in the legend. We will do this later.
public LabelKinds SupportedLabels { get { return LabelKinds.InnerLabel | LabelKinds.ToolTip | LabelKinds.ZAxisLabel; } }
The GetLabel method is the place where we must return the correct label according to the type and position of the label, which are provided as arguments.
public string GetLabel(int index, LabelKinds kind) { double percent = (values[index] / total) * 100; if (kind == LabelKinds.InnerLabel) return percent.ToString("F2") + "%\n" + values[index].ToString(); if (kind == LabelKinds.ToolTip) return "Number of interns: " + values[index].ToString("F0") + "\nPercent of total: " + percent.ToString("F2") + "%"; if (kind == LabelKinds.ZAxisLabel) return _legendLabels[index].ToString(); return null; }
Here we have the chance to work over the raw data that we have for the series and return the desired labels as a string. We want the inner label to appear as the data value together with its percent representation. We calculate the percent thanks to the total variable and format it in an appropriate way:
if (kind == LabelKinds.InnerLabel) return percent.ToString("F2") + "%\n" + values[index].ToString();
We do the same with the tooltips. We add an explanation text to the tooltip of each piece:
if (kind == LabelKinds.ToolTip) return "Number of interns: " + values[index].ToString("F0") + "\nPercent of total: " + percent.ToString("F2") + "%";
The ZAxisLabel is the easiest to do. It will be used by the legend and we perform no special formatting upon it – we just return the label corresponding to the given index.
if (kind == LabelKinds.ZAxisLabel) return _legendLabels[index].ToString();
Among the other notable members of the Series interface are the Dimensions and Title properties. Dimensions is the property that specifies how many data dimensions the series has. They are 1 for charts that use one array of data, 2 for axes that use X and Y data, and 3 for 3D charts, which need X, Y and Z data. In our case we return 1 as property value because pie charts, just like radar charts, use only one data array.
public int Dimensions { get { return 1; } }
The Title property returns the Series title. This is an important property but in our case we will not use it. A common use case for Title is to be rendered in legends. We will not render the Series title in the legend, so we return an empty string.
public string Title { get { return ""; } }
Building the CustomPieSeries is an easy task:
var values = new List { 23, 54, 17, 9 }; pieChart1.Series = new CustomPieSeries( values, new List() { " <1 month", " <=3 months", " <=6 months", " >6 months" } );
We create a new instance of our new class and assign it to the Series property of the PieChart control. We provide a list with the data and the labels that we want to appear as a legend.
III. The Legend
The legend in the PieChart control is regulated by the LegendRenderer property. We set the ShowSeriesElements property to true to let the chart know that we want the data from the series to be rendered as labels and not the title:
pieChart1.LegendRenderer.ShowSeriesElements = true;
Then we use the ElementLabelKind property to specify which labels we want to use in the legend. These are the ZAxisLabels:
pieChart1.LegendRenderer.ElementLabelKind = LabelKinds.ZAxisLabel;
The other properties are self-explanatory. We use the Title property to set the legend title and set a transparent brush for both the Background and the BorderStroke = “Duration”;
pieChart1.LegendRenderer.Background = new SolidBrush(Color.Transparent); pieChart1.LegendRenderer.BorderStroke = new SolidBrush(Color.Transparent);
IV. Styling
Styling the chart is done through the Theme property and through styles. There are different style classes available and in our case we will use the PerElementSeriesStyle class. This class accepts as arguments for the brushes and strokes lists with lists that contain the brushes. Then, it colors each element in each series with the corresponding brush in the array at the corresponding index. Our PieChart needs just one list with brushes and strokes. The stroke thicknesses are also added as nested arrays:
pieChart1.Plot.SeriesStyle = new PerElementSeriesStyle() { Fills = new List<List>() { new List() { new SolidBrush(Color.FromArgb(158, 212, 224)), new SolidBrush(Color.FromArgb(187, 236, 247)), new SolidBrush(Color.FromArgb(212, 204, 196)), new SolidBrush(Color.FromArgb(245, 210, 179)) } }, Strokes = new List<list>() { new List() { new SolidBrush(Color.White) } }, StrokeThicknesses = new List<list>() { new List() { 3.0 } } };
We also set some appearance properties through the Theme field:
pieChart1.Theme.DataLabelsFontSize = 10; pieChart1.Theme.HighlightStroke = new SolidBrush(Color.FromArgb(237, 175, 120)); pieChart1.Theme.HighlightStrokeThickness = 4;
The HighlightStroke is used to mark the selected chart element by mouse hover. The DataLabelsFontSize is used not only by painting the inner labels but also by painting the labels in the legend.
Finally we set the Title of the chart:
pieChart1.Title = "Internship Statistics";
And that is the end of this tutorial. You can download the code for the chart together with the MindFusion.*.dll libraries used from this link:
Pie Chart with Custom Labels Source Code Download
You can refer to MindFusion helpful support team with any technical questions regarding the WinForms Charts & Gauges control. Use the forum at: https://mindfusion.eu/Forum/YaBB.pl?board=mchart_disc
About MindFusion Charts and Gauges for WinForms: MindFusion Charts and Gauges for WinForms offers a long list of features that enables developer to build any type of chart, gauge or dashboard. The smart API of the library provides the option different chart elements like plots, axes, labels, and series to be combined and arranged in order to build any type of custom looking chart. The library also supports out of the box the most common chart types together with a set of their widely used properties. The gauge control is indispensable part of the library and offers a linear and oval gauge together with a variety of samples that provide you with the most common types of gauges used: clock, compass, thermometer, car dashboard and more. More about MindFusion Charts and Gauges for WinForms at: https://mindfusion.eu/winforms-chart.html.