de.kletterfreak98.xmass.ui.WeightChart.java Source code

Java tutorial

Introduction

Here is the source code for de.kletterfreak98.xmass.ui.WeightChart.java

Source

/*
 *  Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 de.kletterfreak98.xmass.ui;

import de.kletterfreak98.xmass.Main;
import de.kletterfreak98.xmass.utils.Prefs;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Locale;
import java.util.ResourceBundle;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.labels.StandardXYToolTipGenerator;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.StandardXYItemRenderer;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.XYDataset;
import org.jfree.ui.RefineryUtilities;

/**
 *
 * @author Dominik Stiller <domi.stiller@googlemail.com>
 */
public class WeightChart extends JFrame {

    private ResourceBundle strings = Main.strings;

    public WeightChart(String title, TimeSeries values) {
        super(title);

        setIconImage(
                new ImageIcon(getClass().getClassLoader().getResource("de/kletterfreak98/xmass/resources/fav.png"))
                        .getImage());

        final String chartTitle = strings.getString("weightcourse");
        final XYDataset dataset = new TimeSeriesCollection(values);

        final JFreeChart chart = ChartFactory.createTimeSeriesChart(chartTitle, strings.getString("date"),
                strings.getString("weightinkg"), dataset, false, true, false);

        final XYPlot plot = chart.getXYPlot();
        plot.setDataset(1, new TimeSeriesCollection(values));
        plot.mapDatasetToRangeAxis(1, 1);
        final XYItemRenderer renderer = plot.getRenderer();
        renderer.setToolTipGenerator(StandardXYToolTipGenerator.getTimeSeriesInstance());
        if (renderer instanceof StandardXYItemRenderer) {
            final StandardXYItemRenderer rr = (StandardXYItemRenderer) renderer;
            rr.setShapesFilled(true);
        }

        final StandardXYItemRenderer renderer2 = new StandardXYItemRenderer();
        renderer2.setSeriesPaint(0, Color.black);
        renderer.setToolTipGenerator(StandardXYToolTipGenerator.getTimeSeriesInstance());
        plot.setRenderer(1, renderer2);

        final DateAxis axis = (DateAxis) plot.getDomainAxis();
        SimpleDateFormat sdf;
        if (Main.settings.getLang().equals(Locale.GERMANY)) {
            sdf = new SimpleDateFormat("dd.MM.yyyy");
            axis.setDateFormatOverride(sdf);
        } else {
            sdf = new SimpleDateFormat("MM/dd/yyyy");
            axis.setDateFormatOverride(sdf);
        }

        final ChartPanel chartPanel = new ChartPanel(chart);
        chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
        JPanel panel = new JPanel();
        JButton close = new JButton(strings.getString("close"));
        close.setPreferredSize(new Dimension(close.getWidth(), 30));
        close.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                dispose();
            }
        });
        panel.setLayout(new BorderLayout());
        panel.add(chartPanel, BorderLayout.CENTER);
        panel.add(close, BorderLayout.SOUTH);
        panel.setPreferredSize(new java.awt.Dimension(500, 270));
        setContentPane(panel);
        setUndecorated(true);
    }

    public void start() {
        JFrame frame = this;
        frame.setSize(new Dimension(1000, 500));
        frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
        RefineryUtilities.centerFrameOnScreen(frame);
        frame.setVisible(true);
    }
}