Line Chart with dual axis

asked 2013-04-18 15:05:48 +0800

Shahbour gravatar image Shahbour


I need to show Chart with two line series but each got independent value axis.

I was able to add a second axis correctly but I was not able to assign it to the second series. The problem is that jfreechart request a second Datasource to bind to axis while in ZK binding the model property can only be done to CategoryModel or XYModel which create two series in same Datasource.

Is there any way I can extend or create a new supported model that will create two data source instead of one so i can do the second axis binding in a clean way.

Note: HiLowModel create two datasources , the second one is for volume and a secondary axis is added to it as i want.

Note: I was able to do the above by creating the chart completely and then return the image to the content of img control in zul but I don't find this way as clean as using chart component plus i lose the tool tips and other features.

Is there any document on how to customize the model so i can create a new type as i want.

Best Regards

delete flag offensive retag edit


I did find a work around but i am not able to post the answer till tomorrow !! cos i am new user :)

Shahbour ( 2013-04-19 11:38:19 +0800 )edit

1 Answer

Sort by » oldest newest most voted

answered 2013-04-23 17:25:00 +0800

Shahbour gravatar image Shahbour


I was able to do that through the engine as below

public class DualAxisChartEngineViewModel extends JFreeChartEngine {

private int _width = 2;
private boolean showLine = true;
private boolean showShape = true;
private TimeSeriesCollection _ds0 = new TimeSeriesCollection();
private TimeSeriesCollection _ds1 = new TimeSeriesCollection();
private String _valueAxis1Label;

public boolean prepareJFreeChart(JFreeChart jfchart, Chart chart) {

    XYPlot plot = (XYPlot) jfchart.getPlot();

    XYLineAndShapeRenderer renderer0 = (XYLineAndShapeRenderer)plot.getRenderer(0);
    NumberAxis rangeaxis0 = (NumberAxis)plot.getRangeAxis(0);

    TimeSeriesCollection ds =   (TimeSeriesCollection) plot.getDataset(0);

    plot.setDataset(0, _ds0);

    for (int i = 0; i < _ds0.getSeriesCount(); i++) {
        renderer0.setSeriesStroke(i, new BasicStroke(_width));
        renderer0.setSeriesShapesVisible(i, showShape);
        renderer0.setSeriesLinesVisible(i, showLine);
    DateAxis domainaxis = (DateAxis) plot.getDomainAxis();
    domainaxis.setDateFormatOverride(new SimpleDateFormat("hh:mma"));

    NumberAxis rangeaxis1 = new NumberAxis(_valueAxis1Label);
    plot.setRangeAxis(1, rangeaxis1);
    plot.setDataset(1, _ds1);
    plot.mapDatasetToRangeAxis(1, 1);
    XYLineAndShapeRenderer renderer1 = new XYLineAndShapeRenderer();
    //renderer2.setSeriesPaint(0, Color.black);
    for (int i = 0; i < _ds1.getSeriesCount(); i++) {
        renderer1.setSeriesStroke(i, new BasicStroke(_width));
        renderer1.setSeriesShapesVisible(i, showShape);
        renderer1.setSeriesLinesVisible(i, showLine);
    plot.setRenderer(1, renderer1);

    return false;

The only weird thing is that even thought i am overwriting both dataset but I had to set the model as below or i would have an error in array index and because I am using two dataset i had to add two series to my fake model

 <chart id="chart" title="Half-Year Report" width="520" height="350" paneColor="#FFFFFF"
    type="time_series" yAxis="Amount" model="@bind(vm._model)"
     engine="@bind(vm._engine)" />

public XYModel getModel() {
       XYModel xymodel = new SimpleXYModel();
        xymodel.addValue("2001", new Integer(20), new Integer(120));
        xymodel.addValue("2002", new Integer(30), new Integer(120));
        return xymodel;

Hope this Help

Best Regards

link publish delete flag offensive edit
Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools




Asked: 2013-04-18 15:05:48 +0800

Seen: 23 times

Last updated: Apr 23 '13

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More