Creating one bar chart from several data sources

Article ID: 2232
Last updated: 29 May, 2018
Article ID: 2232
Last updated: 29 May, 2018
Revision: 3
Views: 781
Posted: 14 Mar, 2011
by Dean J.
Updated: 29 May, 2018
by Gargani A.

Question

How to create one bar chart from several data sources?

Answer

The sample uses several data sources to create one bar chart. The class IlvSingleBarRenderer has the functions setWidthPercent() andsetXShift(). They are used to calculate the offset of each bar.

Main.java

import ilog.views.chart.*;
import ilog.views.chart.data.*;
import ilog.views.chart.renderer.*;
import javax.swing.*;
public class Main
{
public static void main(String[] args)
{
/* Comment out the following line if using JViews Charts 8.6 or earlier
*/
ilog.views.util.IlvProductUtil.DeploymentLicenseRequired(ilog.views.util.IlvProductUtil.IBM_ILOG_JViews_Charts_Deployment );
// Create a window
JFrame window = new JFrame("Main");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setBounds(20, 20, 400, 300);
// Create two data sources
double[][][] values = {
{{6.2, 5.3, 8.5, 9.6}},
{{3.4, 4.6, 0.3, 2.8}} };
IlvDataSource[] sourceArray = new IlvDataSource[values.length];
for(int i = 0; i < sourceArray.length; i++) {
sourceArray[i] = new IlvDefaultDataSource(values[i], -1, null, null);
};
// Create a chart using both data sources, and add it to the window
IlvChart chart = new IlvChart();
chart.addYAxis(true, false);
for(int i = 0; i < sourceArray.length; i++) {
IlvDataSet set = sourceArray[i].getDataSet(0);
IlvSingleBarRenderer renderer = new IlvSingleBarRenderer();
renderer.setDataSource(sourceArray[i]);
renderer.setWidthPercent(80.0 / sourceArray.length);
renderer.setXShift(renderer.getWidth() * .5 * (2*i - sourceArray.length + 1));
chart.addRenderer(renderer, i);
};
window.getContentPane().add(chart);
// Show the window
window.setVisible(true);
}
}

This article was:   Helpful | Not helpful
Report an issue
Article ID: 2232
Last updated: 29 May, 2018
Revision: 3
Views: 781
Posted: 14 Mar, 2011 by Dean J.
Updated: 29 May, 2018 by Gargani A.
Attached files

Also listed in


Others in this category