accounting-manager/src/main/java/org/gcube/portlets/admin/accountingmanager/client/maindata/charts/AccountingChart4Task.java

186 lines
6.2 KiB
Java
Raw Normal View History

package org.gcube.portlets.admin.accountingmanager.client.maindata.charts;
import java.util.Date;
import org.gcube.portlets.admin.accountingmanager.client.state.AccountingStateData;
import org.gcube.portlets.admin.accountingmanager.shared.data.response.SeriesStorage;
import org.gcube.portlets.admin.accountingmanager.shared.data.response.SeriesStorageData;
import org.gcube.portlets.admin.accountingmanager.shared.data.response.SeriesTask;
import org.gcube.portlets.admin.accountingmanager.shared.exception.AccountingManagerChartDrawException;
import com.github.highcharts4gwt.client.view.widget.HighchartsLayoutPanel;
import com.github.highcharts4gwt.model.array.api.ArrayNumber;
import com.github.highcharts4gwt.model.array.api.ArrayString;
import com.github.highcharts4gwt.model.factory.api.HighchartsOptionFactory;
import com.github.highcharts4gwt.model.factory.jso.JsoHighchartsOptionFactory;
import com.github.highcharts4gwt.model.highcharts.option.api.ChartOptions;
import com.github.highcharts4gwt.model.highcharts.option.api.SeriesArea;
import com.github.highcharts4gwt.model.highcharts.option.api.SeriesColumn;
import com.sencha.gxt.widget.core.client.container.MarginData;
import com.sencha.gxt.widget.core.client.container.SimpleContainer;
/**
* Accounting Chart 4 Task
*
* @author "Giancarlo Panichi" email: <a
* href="mailto:g.panichi@isti.cnr.it">g.panichi@isti.cnr.it</a>
*
*/
public class AccountingChart4Task extends AccountingChartBuilder {
private AccountingStateData accountingStateData;
public AccountingChart4Task(AccountingStateData accountingStateData) {
this.accountingStateData = accountingStateData;
}
@Override
public void buildChart() throws AccountingManagerChartDrawException {
AccountingChartPanel accountingChartPanel=null;
if (accountingStateData == null
|| accountingStateData.getAccountingType() == null
|| accountingStateData.getSeriesRequest() == null
|| accountingStateData.getSeriesResponse() == null) {
accountingChartSpec.setChart(accountingChartPanel);
return;
}
if (!(accountingStateData.getSeriesResponse() instanceof SeriesTask)) {
accountingChartSpec.setChart(accountingChartPanel);
return;
}
SeriesStorage seriesStorage = (SeriesStorage) accountingStateData
.getSeriesResponse();
double minRange = ChartTimeMeasure.calculateMinRange(accountingStateData
.getSeriesRequest().getAccountingPeriod());
double interval = ChartTimeMeasure.calculateInterval(accountingStateData
.getSeriesRequest().getAccountingPeriod());
Date dateStart = accountingStateData.getSeriesRequest()
.getAccountingPeriod().getStartDate();
HighchartsOptionFactory highchartsFactory = new JsoHighchartsOptionFactory();
ChartOptions options = highchartsFactory.createChartOptions();
options.chart().zoomType("xy");
options.title().text("Accounting Task");
options.subtitle().text("Click and drag in the plot area to zoom in");
ArrayString colors = options.colors();
// colors.setValue(0, "#cc0038");
// colors.setValue(1, "#32cd32");
// xAxis
options.xAxis().type("datetime").minRange(minRange);
// yAxis
// options.yAxis().title().text("Exchange rate");
// Highcharts.getOptions().colors[0]
String multiAxis = "[{" + " \"id\": \"DataVolume\","
+ " \"labels\": { " + " \"format\": \"{value}\","
+ " \"style\": { " + " \"color\": \""
+ colors.get(1)
+ "\""
+ " }"
+ " },"
+ " \"title\": { "
+ " \"text\": \"Data Volume\","
+ " \"style\": {"
+ " \"color\": \""
+ colors.get(1)
+ "\""
+ " }"
+ " }"
+ "} , {"
+ " \"id\": \"OperationCount\", "
+ " \"title\": {"
+ " \"text\": \"Operation Count\","
+ " \"style\": {"
+ " \"color\": \""
+ colors.get(0)
+ "\""
+ " }"
+ " },"
+ " \"labels\": {"
+ " \"format\": \"{value}\","
+ " \"style\": {"
+ " \"color\": \""
+ colors.get(0)
+ "\""
+ " }"
+ " }," + " \"opposite\": \"true\"" + "}]";
options.setFieldAsJsonObject("yAxis", multiAxis);
// TODO does not seem to be working
String fillcolor = "{" + "\"linearGradient\": {" + "\"x1\": 0,"
+ "\"y1\": 0," + "\"x2\": 0," + "\"y2\": 1" + "},"
+ "\"stops\": [" + "[" + "0, \"#058DC7\"" + "]," + "["
+ "1, \"#FFFFFF\"" + "]" + "]" + "}";
options.plotOptions().area()
.setFieldAsJsonObject("fillColor", fillcolor).marker()
.radius(2).lineWidth(1).states().hover().lineWidth(1);
SeriesColumn seriesDataVolume = highchartsFactory.createSeriesColumn();
seriesDataVolume.name("Data Volume");
seriesDataVolume.color(colors.get(1));
seriesDataVolume.type("column");
ArrayNumber dataDataVolume = seriesDataVolume.dataAsArrayNumber();
seriesDataVolume.pointInterval(interval)
.pointStart(dateStart.getTime());
SeriesArea seriesOperationCount = highchartsFactory.createSeriesArea();
seriesOperationCount.name("Operation Count");
seriesOperationCount.yAxisAsString("OperationCount");
seriesOperationCount.color(colors.get(0));
ArrayNumber dataOperationCount = seriesOperationCount
.dataAsArrayNumber();
seriesOperationCount.pointInterval(interval).pointStart(
dateStart.getTime());
for (SeriesStorageData seriesStorageData : seriesStorage.getSeries()) {
dataDataVolume.push(seriesStorageData.getDataVolume());
dataOperationCount.push(seriesStorageData.getOperationCount());
}
options.series().addToEnd(seriesDataVolume);
options.series().addToEnd(seriesOperationCount);
options.chart().showAxes(true);
options.legend().enabled(true);
// options.legend().layout("vertical");
// options.legend().align("left");
// options.legend().x(120);
// options.legend().verticalAlign("top");
// options.legend().y(100);
// options.legend().floating(true);
// options.legend()
// .backgroundColor(
// "(Highcharts.theme && Highcharts.theme.legendBackgroundColor) || 'white'");
HighchartsLayoutPanel highchartsLayoutPanel = new HighchartsLayoutPanel();
highchartsLayoutPanel.renderChart(options);
SimpleContainer container=new SimpleContainer();
container.add(highchartsLayoutPanel, new MarginData(0));
accountingChartPanel=new AccountingChartPanel(container);
accountingChartSpec.setChart(accountingChartPanel);
}
}