Using Series"

From Documentation
(Created page with "{{ZKChartsEssentialsPageHeader}} __TOC__ Another way to add data to a chart is through <javadoc directory="zkcharts">org.zkoss.chart.Series</javadoc>. You have to use <tt>Seri...")
 
m (correct highlight (via JWB))
 
(5 intermediate revisions by the same user not shown)
Line 4: Line 4:
  
  
Another way to add data to a chart is through <javadoc directory="zkcharts">org.zkoss.chart.Series</javadoc>. You have to use <tt>Series</tt> to combine multiple types of chart in one chart component like [https://www.zkoss.org/zkchartsdemo/combo this demo] because each <tt>Series</tt> can be rendered as different chart types.
+
Another way to add data to a chart is through <javadoc directory="zkcharts">org.zkoss.chart.Series</javadoc>. You have to use <code>Series</code> to combine multiple types of chart in one chart component like [https://www.zkoss.org/zkchartsdemo/combo this demo] because each <code>Series</code> can be rendered as different chart types.
  
You don't need to instantiate <tt>Series</tt> by yourselves, just call <tt>getSeries(index)</tt> like:
+
= Create a Series =
 +
You don't need to instantiate <code>Series</code> by yourselves, just call <code>getSeries(index)</code> and its underlying implementation creates for you:
 
<source lang='java'>
 
<source lang='java'>
 
Charts chart;
 
Charts chart;
 
...
 
...
 
Series series0 = chart.getSeries(0);
 
Series series0 = chart.getSeries(0);
 +
...
 +
Series series1 = chart.getSeries(1);
 +
</source>
 +
 +
 +
= Add Data Into a Series =
 +
Call <code>setData()</code> to add data points to the series, and data could be Double, Integer, Number. If you want to show category, pass <javadoc directory="zkcharts">org.zkoss.chart.Point</javadoc> as parameters.
 +
 +
Here is a simple example.
 +
 +
[https://github.com/zkoss/zkchartsessentials/blob/master/src/main/java/org/zkoss/zkcharts/essentials/SeriesComposer.java SeriesComposer.java]
 +
<source lang='java' highlight='13,14,15,16'>
 +
public class SeriesComposer extends SelectorComposer<Window> {
 +
 +
    @Wire
 +
    Charts chart;
 +
   
 +
    public void doAfterCompose(Window comp) throws Exception {
 +
        super.doAfterCompose(comp);
 +
        initData();
 +
    }
 +
 +
private void initData() {
 +
chart.getXAxis().setType("category");
 +
Series series0 = chart.getSeries(0);
 +
series0.setData(new Point("apples", 5), new Point("pears", 9), new Point("oragnes", 4), new Point("bannas", 8), new Point("grapes", 10));
 +
series0.setType("area");
 +
series0.setName("John");
 +
 +
Series series1 = chart.getSeries(1);
 +
series1.setData(new Point("apples", 2),  new Point("pears", 1),new Point("oragnes", 3), new Point("bannas", 5), new Point("grapes", 9));
 +
series1.setType("column");
 +
series1.setName("Peter");
 +
}
 +
}
 
</source>
 
</source>
 +
* Line 14: If there is missing points, just pass null like <code>new Point("category", null)</code>
 +
 +
 +
= Clear/Remove Series=
  
 +
<source lang='java'>
 +
@Listen("onClick = #clear")
 +
public void clear(){
 +
int seriesSize = chart.getSeriesSize();
 +
for (int i = 0; i < seriesSize; i++){
 +
chart.getSeries().remove();
 +
}
 +
}
 +
</source>
 +
* <code>getSeriesSize()</code> changes for each removal
  
 
{{ZKChartsEssentialsPageFooter}}
 
{{ZKChartsEssentialsPageFooter}}

Latest revision as of 03:09, 18 January 2022


Another way to add data to a chart is through Series. You have to use Series to combine multiple types of chart in one chart component like this demo because each Series can be rendered as different chart types.

Create a Series

You don't need to instantiate Series by yourselves, just call getSeries(index) and its underlying implementation creates for you:

Charts chart;
...
Series series0 = chart.getSeries(0);
...
Series series1 = chart.getSeries(1);


Add Data Into a Series

Call setData() to add data points to the series, and data could be Double, Integer, Number. If you want to show category, pass Point as parameters.

Here is a simple example.

SeriesComposer.java

public class SeriesComposer extends SelectorComposer<Window> {

    @Wire
    Charts chart;
    
    public void doAfterCompose(Window comp) throws Exception {
        super.doAfterCompose(comp);
        initData();
    }

	private void initData() {
		chart.getXAxis().setType("category");
		Series series0 = chart.getSeries(0);
		series0.setData(new Point("apples", 5), new Point("pears", 9), new Point("oragnes", 4), new Point("bannas", 8), new Point("grapes", 10));
		series0.setType("area");
		series0.setName("John");
		
		Series series1 = chart.getSeries(1);
		series1.setData(new Point("apples", 2),  new Point("pears", 1),new Point("oragnes", 3), new Point("bannas", 5), new Point("grapes", 9));
		series1.setType("column");
		series1.setName("Peter");
	}
}
  • Line 14: If there is missing points, just pass null like new Point("category", null)


Clear/Remove Series

	@Listen("onClick = #clear")
	public void clear(){
		int seriesSize = chart.getSeriesSize();
		for (int i = 0; i < seriesSize; i++){
			chart.getSeries().remove();
		}
	}
  • getSeriesSize() changes for each removal

< Get Complete Source Code of This Book >


Last Update : 2022/01/18

Copyright © Potix Corporation. This article is licensed under GNU Free Documentation License.