/*
 * Copyright (c) 2017 by Gerrit Grunwald
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package eu.hansolo.tilesfx.skins;

import eu.hansolo.tilesfx.Tile;
import eu.hansolo.tilesfx.fonts.Fonts;
import eu.hansolo.tilesfx.tools.Helper;
import eu.hansolo.tilesfx.chart.SmoothAreaChart;
import javafx.geometry.Insets;
import javafx.geometry.Side;
import javafx.scene.chart.Axis;
import javafx.scene.text.Text;


/**
 * Created by hansolo on 19.12.16.
 */
public class AreaChartTileSkin extends TileSkin {
    private Text                            titleText;
    private SmoothAreaChart<String, Number> chart;
    private Axis                            xAxis;
    private Axis                            yAxis;


    // ******************** Constructors **************************************
    public AreaChartTileSkin(final Tile TILE) {
        super(TILE);
    }

    // ******************** Initialization ************************************
    @Override protected void initGraphics() {
        super.initGraphics();

        titleText = new Text();
        titleText.setFill(tile.getTitleColor());
        Helper.enableNode(titleText, !tile.getTitle().isEmpty());

        xAxis = tile.getXAxis();
        yAxis = tile.getYAxis();

        chart = new SmoothAreaChart<>(xAxis, yAxis);
        chart.getData().addAll(tile.getSeries());
        chart.setLegendSide(Side.TOP);
        chart.setVerticalZeroLineVisible(false);
        chart.setCreateSymbols(false);

        getPane().getChildren().addAll(titleText, chart);
    }

    @Override protected void registerListeners() {
        super.registerListeners();
    }


    // ******************** Methods *******************************************
    @Override protected void handleEvents(final String EVENT_TYPE) {
        super.handleEvents(EVENT_TYPE);
    }


    // ******************** Resizing ******************************************
    @Override protected void resizeStaticText() {
        double maxWidth = width - size * 0.1;
        double fontSize = size * textSize.factor;

        titleText.setFont(Fonts.latoRegular(fontSize));
        if (titleText.getLayoutBounds().getWidth() > maxWidth) { Helper.adjustTextSize(titleText, maxWidth, fontSize); }

        switch(tile.getTitleAlignment()) {
            default    :
            case LEFT  : titleText.relocate(size * 0.05, size * 0.05); break;
            case CENTER: titleText.relocate((width - titleText.getLayoutBounds().getWidth()) * 0.5, size * 0.05); break;
            case RIGHT : titleText.relocate(width - (size * 0.05) - titleText.getLayoutBounds().getWidth(), size * 0.05); break;
        }
    }

    @Override protected void resize() {
        super.resize();

        chart.setMinSize(width - size * 0.1, height - size * 0.1);
        chart.setPrefSize(width - size * 0.1, height - size * 0.1);
        chart.setMaxSize(width - size * 0.1, height - size * 0.1);
        chart.setPadding(new Insets(titleText.getLayoutBounds().getHeight() + size * 0.05, 0, 0, 0));
        chart.relocate(size * 0.05, size * 0.05);
    }

    @Override protected void redraw() {
        super.redraw();
        titleText.setText(tile.getTitle());

        resizeStaticText();

        titleText.setFill(tile.getTitleColor());
    }
}

@@ -697,59 +697,221 @@
 sfx.
-fonts.Fonts;%0Aimport eu.hansolo.tilesfx.tools.Hel
+chart.ChartData;%0Aimport eu.hansolo.tilesfx.chart.SmoothedChart;%0Aimport eu.hansolo.tilesfx.events.SmoothedChartEvent;%0Aimport eu.hansolo.tilesfx.events.TileEvent;%0Aimport eu.hansolo.tilesfx.events.TileEvent.EventTy
 pe
-r
 ;%0Aim
@@ -938,29 +938,93 @@
 sfx.
-chart.SmoothAreaChart
+fonts.Fonts;%0Aimport eu.hansolo.tilesfx.tools.Helper;%0Aimport javafx.event.EventHandler
 ;%0Aim
@@ -1275,32 +1275,33 @@
                 
+ 
 titleText;%0A    p
@@ -1309,28 +1309,26 @@
 ivate Smooth
-Area
+ed
 Chart%3CString
@@ -1336,16 +1336,19 @@
  Number%3E
+   
  chart;%0A
@@ -1387,24 +1387,25 @@
             
+ 
 xAxis;%0A    p
@@ -1447,15 +1447,85 @@
     
+ 
 yAxis;%0A
+    private EventHandler%3CSmoothedChartEvent%3E chartEventEventHandler;%0A
 %0A%0A  
@@ -1831,24 +1831,158 @@
 raphics();%0A%0A
+        chartEventEventHandler = e -%3E tile.fireTileEvent(new TileEvent(EventType.SELECTED_CHART_DATA, new ChartData(e.getValue())));%0A%0A
         titl
@@ -2210,20 +2210,18 @@
 w Smooth
-Area
+ed
 Chart%3C%3E(
@@ -2281,24 +2281,55 @@
 tSeries());%0A
+        chart.setFilled(true);%0A
         char
@@ -2442,16 +2442,68 @@
 (false);
+%0A        chart.setSnapToTicks(tile.isSnapToTicks());
 %0A%0A      
@@ -2643,24 +2643,113 @@
 isteners();%0A
+        chart.addEventHandler(SmoothedChartEvent.DATA_SELECTED, chartEventEventHandler);%0A
     %7D%0A%0A%0A    
@@ -2932,32 +2932,194 @@
 T_TYPE);%0A    %7D%0A%0A
+    @Override public void dispose() %7B%0A        chart.removeEventHandler(SmoothedChartEvent.DATA_SELECTED, chartEventEventHandler);%0A        super.dispose();%0A    %7D%0A%0A
 %0A    // ********
@@ -4483,16 +4483,144 @@
 cText();
+%0A        chart.setSelectorStrokeColor(tile.getForegroundColor());%0A        chart.setSelectorFillColor(tile.getBackgroundColor());
 %0A%0A      

Make use of the new SmoothedChart incl. interactivity