ASE Home Page Products Download Purchase Support About ASE
ChartDirector Support
Forum HomeForum Home   SearchSearch

Message ListMessage List     Post MessagePost Message

  Combining Bar and line charts
Posted by James Allsopp on Aug-08-2013 00:57
Hi,
I'm trying to make a hybrid graph consisting of a bar chart and a line chart. For example if we had a year's data from two unrelated variables with different units, sampled every hour and I wanted:

i.) A monthly bar chart of the first variable's data with each bar representing the sum of the data points.

ii.) A line plot of the second variable's data going from 1st Jan to 31st Dec, so the part of the line corresponding to approximately one month is the same as a bin width ( approximately due to the effect of the variable month length).

If you need any clarification, just ask,

Thanks,
James

  Re: Combining Bar and line charts
Posted by Peter Kwan on Aug-08-2013 01:14
Hi James,

You can create 4 arrays to represent your data:

(1) An array of date/time representing the x-position of the bars. I suggest you put the bars at the middle of the each month (that is, at Jan 15, Feb 14, Mar 15, Apr 15, .... Dec 15). This array should contain 12 elements.

(Note: The exact meaning of date/time depends on your programming language. It can be System.DateTime for .NET, java.util.Date for Java, or other things for other programming languages. You may look up "Date Representation" from the ChartDirector documentation index to see which date/time representation is suitable for the programming langauge edition of ChartDirector you are using.)

(2) An array of 12 values for the bar data.

(3) An arary of 365/366 date/time for the x-position of the points that will be joined to form the line.

(4) An array of 365/366 values for the line data points.

You can then plot the chart like (in C#):

LineLayer layer = c.addLineLayer(myLineData, ....);
layer.setXData(myLineDates);

BarLayer layer2 = c.addBarLayer(myBarData, ....);
layer2.setXData(myBarDates);

Hope this can help.

Regards
Peter Kwan

  Re: Combining Bar and line charts
Posted by James Allsopp on Aug-08-2013 20:54
Excellent, is there any way I can plot the two layers with different y axes now? I'm programming in C++ btw.

I've tried using setUseYAxis, but there doesn't seem to any constructors available to create an axis in the first place?

James

  Re: Combining Bar and line charts
Posted by Peter Kwan on Aug-08-2013 22:15
Hi James,

There is a sample code called "Dual Y-Axis" that shows how to use two y-axes for two layers. In brief, you just need to call setUseYAxis2 for the layer that you want to use the second y-axis.

layer->setUseYAxis2();

A long time ago, ChartDirector only supports two x-axes and two y-axes. The two y-axes are called c->yAxis() and c->yAxis2(). You may use setUseYAxis2({true|false}) to specify which y-axis a layer use. Later versions of ChartDirector supports unlimited number of y-axis (limited only by the computer memory). If you need more than 2 y-axes, the additional y-axis can be created using XYChart.addAxis. The setUseYAxis can then be used to specify which layer is using which axis.

Hope this can help.

Regards
Peter Kwan

  Re: Combining Bar and line charts
Posted by James Allsopp on Aug-14-2013 23:45
Hi,
Thanks for that. Is there a way then of replacing the x-axis labels and having labels like in a bar chart, for instance, instead of having a time from 1st January to 31st December, you would have Jan, Feb, Mar etc.

Thanks,
James

  Re: Combining Bar and line charts
Posted by Peter Kwan on Aug-15-2013 03:10
Hi James,

I assume you are referring to the labels on the x-axis (which are for human reading), and
not the actual scale of the x-axis. The actual scale of the x-axis should match your data,
otherwise ChartDirector cannot use the x-axis to position your data points.

According to your previous message, the actual scale should be from 1st Jan to 31st
Dec, as it is the actual values used by your line data, and it also includes the values used
by the bar data.

If you do not configure how the x-axis is labelled, ChartDirector will automatically label it.
It may put one label per month, or one label every 2 months, or using other choice. It
depends on how large is your chart. If you want to hard coded it to one label per month,
and the label is just showing the month name, you may use:

//1st Jan 00:00:00 to 31st Dec 59:59:59 with a label every month
c->xAxis()->setDateScale(Chart::chartTime(2012, 1, 1, 0, 0, 0), Chart::chartTime(2012,
12, 31, 59, 59, 59), 86400 * 30);
//The label shows the name of the month
c->xAxis()->setLabelFormat("{value|mmm}");

By default, the label will be at the start of the month. If you want to put the label at the
middle of the month, you may use offset the label by 15 days (1 day = 86400 seconds):

c->xAxis()->setLabelOffset(86400 * 15);

Hope this can help.

Regards
Peter Kwan