Building a Mekko Chart In React

In this tutorial we will use the BarChart control from the JavaScript Charting library to build a mekko chart in React. The final chart is shown below:

I. Project Setup

We create a blank React app using create-react-app:

npx create-react-app mekko-chart

After that we navigate to the newly created folder with the project and there install jQuery and the charting library for JavaScript from npm:

cd mekko-chart
npm install jquery
npm install chart-library

Now we are ready to code. We create a new file in the src folder, which we name MekkoChart. There we add references to React, jQuery and the chart library:

import React, { Component } from 'react';
import * as Charting from 'chart-library';
import MindFusion from 'mindfusion-common';

This means we can access now the members of the chart library using the “Charting” prefix. The “common” namespace contains graphical classes like brushes, strokes, font properties etc. That’s why we import it as well. Its members will be accessed with the “Drawing” prefix.

The chart library is a JavaScript library and it requires a DOM element to render itself onto. We get the DOM element in the constructor of the MekkoChart class, which we initialize:

class MekkoChart extends Component {

constructor(props) {

    this.el = React.createRef();

export default MekkoChart;

Now that we have a reference to the underlying DOM element, we have to declare which is it. In the render method we create a Canvas element that will render our chart. We use the ref attribute to assign to it the reference that was returned by createRef:

render() {
     return (
    <canvas ref="{this.el}" width="1000px" height="500px"></canvas>

With that we are ready to create the chart and customize it.

II. The Chart and Chart Data

The chart will be instance of the stacked BarChart . The constructor requires a reference to the DOM element and the type of bar chart to create. We will use BarLayout to indicate that that bars will be rendered one on top the other:

var chart = new Charting.Controls.BarChart(this.el.current, Charting.BarLayout.Stack);
chart.licenseLocation = './chart_lic.txt'; 
chart.barSpacingRatio = 0;

If you have purchased a license for the Charting library for JavaScript, you can set the location of the file with the license key using the licenseLocation property.

The barSpacingRatio property indicates the space between bars as a percent from the bar width. In our case we don’t want any space between the bars and we set it to 0.

There are various classes, which you can use to specify data for the chart. Each class derives from the Series interface. In our case we use the BarSeries and SimpleSeries classes. We use BarSeriesm because the last parameter in the constructor is for labels at the XAxis. We want under each bar to have labels and this parameter is exactly what we need. The other series needs not to be a BarSeries , so we use SimpleSeries .

The SimpleSeries constructor accepts two parameters: one is a list with the data and the other list with the labels. We provide the same list because we want the data to be rendered as labels as well:

// create the data series
var series = new MindFusion.Charting.Collections.ObservableCollection(
	new Charting.SimpleSeries(new MindFusion.Charting.Collections.List([23, 74, 58, 62, 22, 29]), 
	new MindFusion.Charting.Collections.List([23, 74, 58, 62, 22, 29])),
	new Charting.BarSeries(new MindFusion.Charting.Collections.List([77, 26, 42, 38, 78, 71 ]),
	new MindFusion.Charting.Collections.List([77, 26, 42, 38, 78, 71 ]), null, labels)		

The BarSeries constructor accepts 4 parameters: the first one is a list with the bar data. The others are labels: inner, top and XAxis-labels. We specify a list with the data and set the same data list as the second parameter. The third parameter is null, but the last parameter is a list with custom labels:

var labels = ["Biology", "Math", "Chemistry", "IT", "Foreign\nLanguages", "Economics"];

Now we have to tell the library which list is used for what kind of labels. Each chart can have a variety of labels and the supportedLabels property is how you specify where you want the labels to be rendered. In our case we want the labels of the simple series to be rendered as tooltips:

series.item(0).title = "Boys";
series.item(0).supportedLabels = Charting.LabelKinds.ToolTip;

The LabelKinds enumeration supports bitwise combination of its members, which is used when the series supports multiple labels. The BarSeries is such series:

series.item(1).supportedLabels = Charting.LabelKinds.ToolTip | Charting.LabelKinds.XAxisLabel;
series.item(1).title = "Girls";

With this property we indicate that the two lists with labels will be used for tooltips and labels for the XAxis. The Title property of each Series is used for a label in the chart legend.

Finally, we need to assign the ObservableCollection with the Series to the series property of the Chart class:

chart.series = series;

III. Axes and Appearance

The two axes of our chart are accessible through the xAxis and yAxis properties of the BarChart class. They expose various fields for customizing the axis in terms of scale: interval , minValue , maxValue and more. The title property specifies the main label for the axis.

//axis settings
chart.yAxis.title = "Percent";
chart.yAxis.interval = 10;
chart.yAxis.maxValue = 101;

chart.xAxis.interval = 1;
chart.xAxis.minValue = 0;
chart.xAxis.maxValue = 7;
chart.xAxis.title = "";

The xAxisTickLength and yAxisTickLength properties of BarChart set how long the ticks will be. We don’t want any ticks on the XAxis. We also want to hide the coordinates on this axis – we want it to render only labels:

 //chart settings
chart.showXCoordinates = false;
chart.xAxisTickLength = 0;		
chart.showToolTips = true;
chart.showLegendTitle = false;

The appearance of the bars is specified through various SeriesStyle classes. We use the PerSeriesStyle class, which applies the same brush and stroke to all elements of a consequtive Series from the series list. The brushes, strokes and the stroke thicknesses are provided as parameters:

// assign one brush per series
		chart.plot.seriesStyle = new Charting.PerSeriesStyle(new MindFusion.Charting.Collections.List([firstBrush, secondBrush]),
		new MindFusion.Charting.Collections.List([thirdBrush]), new MindFusion.Charting.Collections.List([6]));

The theme property of the Chart class is the property that exposes the most appearance settings for any type of chart. As you can see we have used it to specify how the labels, legend and the highlight of the chart will look. The highlightStroke is the brush that is used to outline the chart element that is currently pointed with the mouse:

//appearance settings
chart.theme.legendBackground = thirdBrush;
chart.theme.legendBorderStroke = darkGrayBrush;
chart.theme.dataLabelsBrush = darkGrayBrush;
chart.theme.dataLabelsFontSize = chart.theme.axisLabelsFontSize = 14;
chart.theme.axisTitleFontSize = 16;
chart.theme.axisLabelsFontSize = 14;
chart.theme.axisTitleFontName = "Verdana";
chart.theme.axisLabelsFontName = "Verdana";
chart.theme.dataLabelsFontName = "Verdana";
chart.theme.titleBrush = new MindFusion.Charting.Drawing.Brush("#737373");
chart.theme.titleFontName = "Verdana";
chart.theme.highlightStroke = new MindFusion.Charting.Drawing.Brush("#5870a7");
chart.theme.highlightStrokeThickness = 3;
chart.theme.highlightStrokeDashStyle = Charting.Drawing.DashStyle.Dot;

The Theme class has many members, all of them used to customize the chart looks. You can create custom themes and apply them directly to the theme property as objects.

The last thing to customize is the ToolTip . Tooltip customization is done through properties of a class with the same name:

// customize tooltips
Charting.ToolTip.position = new MindFusion.Drawing.Point (20, 20);
Charting.ToolTip.brush = new MindFusion.Charting.Drawing.Brush("#fafafa");
Charting.ToolTip.pen = new MindFusion.Charting.Drawing.Pen("#9caac6");
Charting.ToolTip.textBrush = darkGrayBrush;
Charting.ToolTip.horizontalPadding = 6;
Charting.ToolTip.verticalPadding = 4;
Charting.ToolTip.font = new Charting.Drawing.Font("Verdana", 12, Charting.Drawing.FontStyle.Regular);

This is a static class and setting the properties is applied directly on the tooltips, even when they are not set through ToolTip.text.

Finally, let’s not forget to call the draw method to make sure that all changes are reflected and rendered:


And with that our Mekko chart is ready. You can download the React project from this link:

Mekko Chart in React: Sample Source Code Download

In order to run the project you need to have npm installed. Open command prompt/terminal, navigate to the project folder and execute:

npm install
npm start

Technical support is available at MindFusion discussion board.

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