package com.ht.ui.chart;

/**
 * Copyright (c) 2008, 2012 Oracle and/or its affiliates.
 * All rights reserved. Use is subject to license terms.
 */
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.stage.Stage;

import com.ht.ui.chart.AdvCmpChart.AdvCmpChartExtraValues;

/**
 * A custom candlestick chart.
 * 
 * @see javafx.scene.chart.Axis
 * @see javafx.scene.chart.Chart
 * @see javafx.scene.chart.NumberAxis
 * @see javafx.scene.chart.XYChart
 */
public class AdvCmpBarChartDemo extends Application {

	// DAY, OPEN, CLOSE, HIGH, LOW, AVERAGE
	private static double[][] data = new double[][] {
			{ 1, 25, 20, 22, 16, 20 }, { 2, 26, 20, 23, 22, 25 },
			{ 3, 30, 28, 20, 20, 32 }, { 4, 24, 20, 24, 22, 30 },
			{ 5, 26, 26, 20, 24, 32 }, { 6, 28, 25, 25, 25, 34 },
			{ 7, 30, 22, 24, 28, 39 }, { 8, 30, 28, 26, 16, 31 }
	// {9, 40, 50, 52, 36, 41},
	// {10, 30, 34, 38, 28, 36},
	// {11, 24, 12, 30, 8, 32.4},
	// {12, 28, 40, 46, 25, 31.6},
	// {13, 28, 18, 36, 14, 32.6},
	// {14, 38, 30, 40, 26, 30.6},
	// {15, 28, 33, 40, 28, 30.6},
	// {16, 25, 10, 32, 6, 30.1},
	// {17, 26, 30, 42, 18, 27.3},
	// {18, 20, 18, 30, 10, 21.9},
	// {19, 20, 10, 30, 5, 21.9},
	// {20, 26, 16, 32, 10, 17.9},
	// {21, 38, 40, 44, 32, 18.9},
	// {22, 26, 40, 41, 12, 18.9},
	// {23, 30, 18, 34, 10, 18.9},
	// {24, 12, 23, 26, 12, 18.2},
	// {25, 30, 40, 45, 16, 18.9},
	// {26, 25, 35, 38, 20, 21.4},
	// {27, 24, 12, 30, 8, 19.6},
	// {28, 23, 44, 46, 15, 22.2},
	// {29, 28, 18, 30, 12, 23},
	// {30, 28, 18, 30, 12, 23.2},
	// {31, 28, 18, 30, 12, 22}
	};

	private void init(Stage primaryStage) {

		Group root = new Group();

		Scene scene = new Scene(root);
		scene.getStylesheets().addAll(
				getClass().getResource("custom-chart.css").toExternalForm());

		primaryStage.setScene(scene);
		root.getChildren().add(createChart());
	}

	protected AdvCmpChart createChart() {
		// final NumberAxis xAxis = new NumberAxis(0,32,1);
		final NumberAxis xAxis = new NumberAxis();
		xAxis.setTickUnit(1);
		xAxis.setMinorTickCount(0);

		final NumberAxis yAxis = new NumberAxis();
		yAxis.setAutoRanging(false);
//		yAxis.setUpperBound(50);

		final AdvCmpChart bc = new AdvCmpChart(xAxis, yAxis);
		// setup chart
		bc.setTitle("Custom Candle Stick Chart");
		xAxis.setLabel("Day");
		yAxis.setLabel("Price");
		// add starting data
		XYChart.Series<Number, Number> series = new XYChart.Series<Number, Number>();
		series.setName("aaaa");
		for (int i = 0; i < data.length; i++) {
			double[] day = data[i];
			series.getData().add(
					new XYChart.Data<Number, Number>(day[0], day[1],
							new AdvCmpChartExtraValues(day[2], day[3])));
		}
		
		bc.setData(FXCollections.observableArrayList(series));
//		ObservableList<XYChart.Series<Number, Number>> data = bc.getData();
//		System.out.println(data);
//		if (data == null) {
//			data = FXCollections.observableArrayList(series);
//			bc.setData(data);
//		} else {
//			bc.getData().add(series);
//		}
		return bc;
	}

	

	@Override
	public void start(Stage primaryStage) throws Exception {
		init(primaryStage);
		primaryStage.show();
	}

	public static void main(String[] args) {
		launch(args);
	}
}
