package com.googlecode.gchart.gcharttestapp.client;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.DeferredCommand;
import com.google.gwt.user.client.ui.Button;
import com.googlecode.gchart.client.GChart;
/** Combination chart containing pie, line, and bar-based curves */
public class GChartExample11 extends GChart {
final double INITIAL_PRICE = 100;
final double MAX_MONTHLY_RELATIVE_CHANGE = 0.2;
final int N_FORCASTED_MONTHS = 13;
double[] prices = new double[N_FORCASTED_MONTHS];
double minPrice = INITIAL_PRICE;
double maxPrice = INITIAL_PRICE;
final static int REPS = 1000;
boolean firstTime = true;
// updates the chart with results of a new oil price simulation
private void updateChart() {
minPrice = INITIAL_PRICE;
maxPrice = INITIAL_PRICE;
prices[0] = INITIAL_PRICE;
for (int i=1; i < N_FORCASTED_MONTHS; i++) {
prices[i] = prices[i-1] *
(1 + MAX_MONTHLY_RELATIVE_CHANGE*(2*GChartTestApp.rnd()-1));
minPrice = Math.min(minPrice, prices[i]);
maxPrice = Math.max(maxPrice, prices[i]);
}
// update pie slice sizes to reflect new min and max
getCurve(0).getSymbol().setPieSliceSize(
minPrice/(minPrice+maxPrice));
getCurve(0).getSymbol().setHovertextTemplate(
GChart.formatAsHovertext("Minimum price (per barrel): "+
getYAxis().formatAsTickLabel(minPrice)));
getCurve(1).getSymbol().setPieSliceSize(
maxPrice/(minPrice+maxPrice));
getCurve(1).getSymbol().setHovertextTemplate(
GChart.formatAsHovertext(
"Maximum price (per barrel): " +
getYAxis().formatAsTickLabel(maxPrice)));
// update backward-price-difference and price curves
getCurve(2).clearPoints();
getCurve(3).clearPoints();
for (int i = 0; i < N_FORCASTED_MONTHS; i++) {
getCurve(2).addPoint(i, (i == 0)?0:(prices[i]-prices[i-1]));
getCurve(3).addPoint(i,prices[i]);
if (prices[i]!=minPrice && prices[i]!=maxPrice) {
//no min/max;
getCurve(3).getPoint().setAnnotationText(null); //no label
}
else {
// label point to indicate it's at a min or max price
getCurve(3).getPoint().setAnnotationFontSize(10);
getCurve(3).getPoint().setAnnotationFontWeight("bold");
if (prices[i]==minPrice) {
getCurve(3).getPoint().setAnnotationLocation(
AnnotationLocation.SOUTH);
getCurve(3).getPoint().setAnnotationText("min");
getCurve(3).getPoint().setAnnotationFontColor("blue");
}
else {
getCurve(3).getPoint().setAnnotationLocation(
AnnotationLocation.NORTH);
getCurve(3).getPoint().setAnnotationText("max");
getCurve(3).getPoint().setAnnotationFontColor("blue");
}
}
}
if (firstTime)
firstTime = false;
else
update();
}
GChartExample11(int id, int borderWidth, boolean forMemory) {
// id==3 x,y,y2 dynamic; id==2 y,y2; id==1 y2; id==0 fixed axes
// misc chart configuration
setOptimizeForMemory(forMemory);
setChartSize(300, 300);
setPlotAreaBackgroundColor("#CCC");
setLegendBackgroundColor(getPlotAreaBackgroundColor());
// setShowOffChartPoints(true);
setGridColor("white");
// convenience methods; these properties could also have been defined
// via CSS. See the javadoc comment for GChart.USE_CSS for more info.
setBackgroundColor("#DDF");
setBorderColor("black");
setBorderWidth("1px");
setBorderStyle("outset");
setFontFamily("Veranda, Arial, sans-serif");
setPadding("5px");
// title and footnotes (w. update button)
setChartTitle(
"Estimated Future Oil Prices " +
"
" +
"All results are pseudo-random. " +
"Randomize fully before you invest." +
"
id =" + id + " reps="+REPS+" optimizeForMemory=" + forMemory + "");
final Button updateButton = new Button(
"Update Estimates");
updateButton.setTitle(
"Click for new totally unbiased, totally random, estimates.");
updateButton.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
for (int i=0; i < REPS; i++) {
DeferredCommand.addCommand(new Command() {
public void execute() {
updateChart();
}
});
}
updateButton.setFocus(true);
}
});
setChartFootnotes(updateButton);
// x-axis config
getXAxis().setAxisLabel(
"time (months from now)
");
getXAxis().setTickCount(13);
getXAxis().setTicksPerLabel(2);
getXAxis().setAxisMin(0);
getXAxis().setAxisMax(N_FORCASTED_MONTHS-1);
if (id == 1)
getXAxis().setAxisMin(Double.NaN);
getXAxis().setHasGridlines(true);
// getXAxis().setTickLabelThickness(10);
// y-axis config
getYAxis().setAxisLabel(
"