CandleStick Chart with customized colors


Advertisements

Following is an example of a CandleStick Chart.

We have already seen the configurations used to draw a chart in Google Charts Configuration Syntax chapter. Now, let us see an example of a CandleStick Chart.

Configurations

We've used CandlestickChartOptions class to customized color of CandleStick Chart.

// Set options
CandlestickChartOptions options = CandlestickChartOptions.create();
options.setLegend(Legend.create(LegendPosition.NONE));
options.setFallingColor(BackgroundColor.create(null, "red", 2));
options.setRisingColor(BackgroundColor.create(null, "orange", 2));

Example

HelloWorld.java

package com.tutorialspoint.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.RootPanel;

import com.googlecode.gwt.charts.client.ChartLoader;
import com.googlecode.gwt.charts.client.ChartPackage;
import com.googlecode.gwt.charts.client.ColumnType;
import com.googlecode.gwt.charts.client.DataTable;
import com.googlecode.gwt.charts.client.corechart.CandlestickChart;
import com.googlecode.gwt.charts.client.corechart.CandlestickChartOptions;
import com.googlecode.gwt.charts.client.options.BackgroundColor;
import com.googlecode.gwt.charts.client.options.Legend;
import com.googlecode.gwt.charts.client.options.LegendPosition;

public class HelloWorld implements EntryPoint {
   private CandlestickChart chart;

   private void initialize() {
      ChartLoader chartLoader = new ChartLoader(ChartPackage.CORECHART);
      chartLoader.loadApi(new Runnable() {
         public void run() {
            // Create and attach the chart
            chart = new CandlestickChart();
            RootPanel.get().add(chart);
            draw();
         }
      });
   }
   private void draw() {
      DataTable dataTable = DataTable.create();
      dataTable.addColumn(ColumnType.STRING, "Date");
      dataTable.addColumn(ColumnType.NUMBER, "A");
      dataTable.addColumn(ColumnType.NUMBER, "B");
      dataTable.addColumn(ColumnType.NUMBER, "C");
      dataTable.addColumn(ColumnType.NUMBER, "D");

      dataTable.addRow("Mon", 20, 28, 38, 45);
      dataTable.addRow("Tue", 31, 38, 55, 66);
      dataTable.addRow("Wed", 50, 55, 77, 80);
      dataTable.addRow("Thu", 77, 77, 66, 50);
      dataTable.addRow("Fri", 68, 66, 22, 15);

      // Set options
      CandlestickChartOptions options = CandlestickChartOptions.create();
      options.setLegend(Legend.create(LegendPosition.NONE));
      options.setFallingColor(BackgroundColor.create(null, "red", 2));
      options.setRisingColor(BackgroundColor.create(null, "orange", 2));

      // Draw the chart
      chart.draw(dataTable,options);
      chart.setWidth("400px");
      chart.setHeight("400px");
   }
   public void onModuleLoad() {
      initialize();
   }
}

Result

Verify the result.

CandleStick Chart with Customized Colors
gwt_googlecharts_candlestick_charts.htm
Advertisements