package com.jidesoft.chart.support.ortelius;

import com.jidesoft.gauge.*;
import com.jidesoft.icons.JideIconsFactory;
import com.jidesoft.chart.Chart;
import com.jidesoft.chart.axis.NumericAxis;

import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

public class GaugeWithColorGrading extends JPanel {

    private Dial dial;

    public GaugeWithColorGrading() {
        super(new BorderLayout());
        dial = new Dial();
        DialFrame frame = new DialFrame();
        frame.setFill(Color.darkGray);
        frame.setFrameWidth(0.02f);
        frame.setMidChordRadius(0.15f);
        frame.setArcEndAngle(9f);
        dial.setFrame(frame);
        DialAxis axis = new DialAxis(0, 100, 10, 5);
        axis.setLabelRadius(0.8);
        axis.setInnerRadius(0.96);
        axis.setOuterRadius(1.0);
        axis.setTickLabelFont(UIManager.getFont("Label.font").deriveFont(24f));
        GaugeTickStyle tickStyle = new GaugeTickStyle();
        axis.setMajorTickStyle(tickStyle);
        axis.setStartAngle(180);
        axis.setEndAngle(0);
        dial.setAxis(axis);
        dial.setStartAngle(180);
        dial.setEndAngle(0);
        dial.setBorder(new EmptyBorder(30,30,30,30));
        addMarker(-50, 60, Color.red);
        addMarker(60, 80, Color.orange);
        addMarker(80, 150, Color.green.darker());
        // Add a pivot
        Pivot pivot = new Pivot(dial, 0.14f, Color.black);
        dial.addDrawable(pivot);
        Paint pivotPaint = new DialRadialPaint(dial, new float[] {0f, 0.09f}, new Color[] {Color.white, Color.darkGray});
        Pivot innerPivot = new Pivot(dial, 0.09f, pivotPaint);
        innerPivot.setZOrder(101);
        dial.addDrawable(innerPivot);
        // Now add a needle
        NeedleStyle needleStyle = new NeedleStyle();
        needleStyle.setHeadLength(0.95);
        needleStyle.setBaseWidth(0.15f);
        needleStyle.setFillPaint(Color.black);
        dial.addNeedle("value", needleStyle);
        dial.setValue("value", 92);
        add(dial, BorderLayout.CENTER);
    }

    private void addMarker(double from, double to, Color color) {
        Paint paint = new DialRadialPaint(dial, new float[] {0f, 0.85f, 1f}, new Color[] {Color.white, color.brighter(), color.darker()});
        DialIntervalMarker marker = new DialIntervalMarker(dial, from, to, paint);
        marker.setInnerRadius(0);
        dial.addDrawable(marker);
    }

    public void paintComponent(Graphics g) {
        float height = getHeight();
        Paint paint = new LinearGradientPaint(0f, 0f, 0f, height, new float[] {0f, 1f}, new Color[] {Color.white, Color.gray});
        Graphics2D g2d = (Graphics2D) g.create();
        g2d.setPaint(paint);
        Rectangle rect = getBounds();
        g2d.fill(rect);
        g2d.dispose();
    }

    public static void main(String[] args) throws Exception {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JFrame frame = new JFrame("GaugeWithColorGrading");
                frame.setIconImage(JideIconsFactory.getImageIcon(JideIconsFactory.JIDE32).getImage());
                frame.setContentPane(new GaugeWithColorGrading());
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setBounds(150, 150, 400, 300);
                frame.setVisible(true);
            }
        });
    }
}