/*
 * Copyright (c) 2009 VMware, Inc.
 * Copyright 2009 G. Brown, ixnay.biz.
 *
 * 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 biz.ixnay.pivot.charts.skin.jfree;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.entity.CategoryItemEntity;
import org.jfree.chart.entity.ChartEntity;
import org.jfree.chart.entity.XYItemEntity;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.CategoryDataset;

import org.apache.pivot.charts.ChartView;
import org.apache.pivot.charts.LineChartView;
import org.apache.pivot.collections.List;

/**
 * Line chart view skin.
 *
 * @author gbrown
 */
public class LineChartViewSkin extends JFreeChartViewSkin {
    private boolean threeDimensional = false;
    private double categoryLabelRotation = 0;

    @Override
    public ChartView.Element getElementAt(int x, int y) {
        ChartView.Element element = null;

        ChartEntity chartEntity = getChartEntityAt(x, y);

        if (chartEntity instanceof CategoryItemEntity) {
            CategoryItemEntity categoryItemEntity = (CategoryItemEntity)chartEntity;
            CategoryDataset dataset = categoryItemEntity.getDataset();

            String columnKey = (String)categoryItemEntity.getColumnKey();
            int columnIndex = dataset.getColumnIndex(columnKey);

            String rowKey = (String)categoryItemEntity.getRowKey();
            int rowIndex = dataset.getRowIndex(rowKey);

            element = new ChartView.Element(rowIndex, columnIndex);
        } else if (chartEntity instanceof XYItemEntity) {
            XYItemEntity xyItemEntity = (XYItemEntity)chartEntity;
            element = new ChartView.Element(xyItemEntity.getSeriesIndex(),
                xyItemEntity.getItem());
        }

        return element;
    }

    @Override
    protected JFreeChart createChart() {
        LineChartView chartView = (LineChartView)getComponent();

        String title = chartView.getTitle();
        String horizontalAxisLabel = chartView.getHorizontalAxisLabel();
        String verticalAxisLabel = chartView.getVerticalAxisLabel();
        boolean showLegend = chartView.getShowLegend();

        String seriesNameKey = chartView.getSeriesNameKey();
        List<?> chartData = chartView.getChartData();

        JFreeChart chart;
        ChartView.CategorySequence categories = chartView.getCategories();
        if (categories.getLength() > 0) {
            CategorySeriesDataset dataset = new CategorySeriesDataset(categories, seriesNameKey, chartData);

            if (threeDimensional) {
                chart = ChartFactory.createLineChart3D(title, horizontalAxisLabel, verticalAxisLabel,
                    dataset, PlotOrientation.VERTICAL, showLegend, false, false);
            } else {
                chart = ChartFactory.createLineChart(title, horizontalAxisLabel, verticalAxisLabel,
                    dataset, PlotOrientation.VERTICAL, showLegend, false, false);
            }

            CategoryPlot plot = (CategoryPlot)chart.getPlot();
            CategoryAxis domainAxis = plot.getDomainAxis();
            CategoryLabelPositions categoryLabelPositions =
                CategoryLabelPositions.createUpRotationLabelPositions(categoryLabelRotation);
            domainAxis.setCategoryLabelPositions(categoryLabelPositions);
        } else {
            chart = ChartFactory.createXYLineChart(title, horizontalAxisLabel, verticalAxisLabel,
                new XYSeriesDataset(seriesNameKey, chartData),
                PlotOrientation.VERTICAL, showLegend, false, false);
        }

        return chart;
    }

    public boolean isThreeDimensional() {
        return threeDimensional;
    }

    public void setThreeDimensional(boolean threeDimensional) {
        this.threeDimensional = threeDimensional;
        repaintComponent();
    }

    public double getCategoryLabelRotation() {
        return categoryLabelRotation;
    }

    public void setCategoryLabelRotation(double categoryLabelRotation) {
        this.categoryLabelRotation = categoryLabelRotation;
        repaintComponent();
    }
}

@@ -842,16 +842,55 @@
 itions;%0A
+import org.jfree.chart.axis.ValueAxis;%0A
 import o
@@ -1105,16 +1105,81 @@
 tation;%0A
+import org.jfree.chart.plot.XYPlot;%0Aimport org.jfree.data.Range;%0A
 import o
@@ -1555,16 +1555,57 @@
 ion = 0;
+%0A    private Range rangeAxisRange = null;
 %0A%0A    @O
@@ -4124,16 +4124,184 @@
 itions);
+%0A%0A            if (rangeAxisRange != null) %7B%0A                ValueAxis rangeAxis = plot.getRangeAxis();%0A                rangeAxis.setRange(rangeAxisRange);%0A            %7D
 %0A       
@@ -4531,32 +4531,255 @@
 , false, false);
+%0A%0A            if (rangeAxisRange != null) %7B%0A                XYPlot plot = (XYPlot)chart.getPlot();%0A                ValueAxis rangeAxis = plot.getRangeAxis();%0A                rangeAxis.setRange(rangeAxisRange);%0A            %7D
 %0A        %7D%0A%0A    
@@ -5287,16 +5287,607 @@
 nent();%0A
-
     %7D%0A
+%0A    public void setRangeAxisLowerBound(double lower) %7B%0A        this.rangeAxisRange = new Range(lower, getRangeAxisUpperBound());%0A        repaintComponent();%0A    %7D%0A%0A    public double getRangeAxisLowerBound() %7B%0A        return rangeAxisRange == null ? 0.0d : rangeAxisRange.getLowerBound();%0A    %7D%0A%0A    public void setRangeAxisUpperBound(double upper) %7B%0A        this.rangeAxisRange = new Range(getRangeAxisLowerBound(), upper);%0A        repaintComponent();%0A    %7D%0A%0A    public double getRangeAxisUpperBound() %7B%0A        return rangeAxisRange == null ? 1.0d : rangeAxisRange.getUpperBound();%0A    %7D%0A%0A
 %7D%0A

Add the ability to control the range axis lower and upper bounds for line charts using the new "rangeAxisLowerBound" and "rangeAxisUpperBound" styles.