0

如果用户按下按钮“F1”代表左,“F2”代表右,我想在 x 轴上向左或向右更改垂直线,依此类推每个参数。这 4 个参数是 ABC 和 D,它们是根据以 csv 文件形式提供给软件的输入计算得出的。

样条图.java

public class SplineChart extends JFrame implements KeyListener {

    JFreeChart chart;
    XYSeriesCollection dataset;
    int count = 0;
    private ChartPanel chartPanel;

    public SplineChart(String applicationTitle, String chartTitle,
            XYSeriesCollection dataset) {
        super(applicationTitle);
        this.addKeyListener(this);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.dataset = dataset;
        drawChart();
        chartPanel = new ChartPanel(chart);
        chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
        setContentPane(chartPanel);
    }

    private void saveImage() throws Exception {
        DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy HH-mm");
        Calendar cal = Calendar.getInstance();
        File file = new File(dateFormat.format(cal.getTime()) + ".jpeg");
        if (!file.exists()) {
            file.createNewFile();
        }
        else {
            file = new File(dateFormat.format(cal.getTime()) + " (" + ++count + ")" + ".jpeg");
        }
        ChartUtilities.saveChartAsJPEG(file, chart, 800, 600);
    }

    @Override
    public void keyPressed(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_F1) {
            FileOpen.a = FileOpen.a - 1;
            FileOpen.atorque = FileOpen.torque[FileOpen.a];
            this.dataset = FileOpen.updateDataset();
            drawChart();
        }

        if (e.getKeyCode() == KeyEvent.VK_F2) {
            FileOpen.a = FileOpen.a + 1;
            FileOpen.atorque = FileOpen.torque[FileOpen.a];
            this.dataset = FileOpen.updateDataset();
            System.out.println("F2 pressed");
            drawChart();
        }

        if (e.getKeyCode() == KeyEvent.VK_S) {
            try {
                saveImage();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }

    }

    @Override
    public void keyReleased(KeyEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void keyTyped(KeyEvent e) {
        // TODO Auto-generated method stub

    }

    public void drawChart() {

        chart = ChartFactory.createXYLineChart("MASTER BLEND", // chart
                // title
                "TIME", // domain axis label
                "TORQUE, TEMPERETURE, SPEED", // range axis label
                this.dataset, // data
                PlotOrientation.VERTICAL, // orientation
                false, // include legend
                true, // tooltips
                false // urls
                );

        String text = String.format("%" + 50 + "s", "");
        XYPlot plot = (XYPlot) chart.getXYPlot();
        TextTitle legendText = new TextTitle("INTEGRATION / ENERGY ");
        legendText.setPosition(RectangleEdge.BOTTOM);
        chart.addSubtitle(legendText);
        legendText = new TextTitle("Maximum :" + text + "D" + text + FileOpen.d
                + text + FileOpen.dtorque + text + FileOpen.dtemp);
        legendText.setPosition(RectangleEdge.BOTTOM);
        legendText.setHorizontalAlignment(HorizontalAlignment.LEFT);
        chart.addSubtitle(legendText);
        legendText = new TextTitle("Maximum :" + text + "X" + text + FileOpen.x
                + text + FileOpen.xtorque + text + FileOpen.xtemp);
        legendText.setPosition(RectangleEdge.BOTTOM);
        legendText.setHorizontalAlignment(HorizontalAlignment.LEFT);
        chart.addSubtitle(legendText);
        legendText = new TextTitle("Minimum :" + text + "B" + text + FileOpen.b
                + text + FileOpen.btorque + text + FileOpen.btemp);
        legendText.setPosition(RectangleEdge.BOTTOM);
        legendText.setHorizontalAlignment(HorizontalAlignment.LEFT);
        chart.addSubtitle(legendText);
        legendText = new TextTitle("Loading Peak :" + text + "A" + text
                + FileOpen.a + text + FileOpen.atorque + text + FileOpen.atemp);
        legendText.setPosition(RectangleEdge.BOTTOM);
        legendText.setHorizontalAlignment(HorizontalAlignment.LEFT);
        chart.addSubtitle(legendText);
        legendText = new TextTitle("Name" + text + text + "Time" + text
                + "Torque [Nm]" + text + "Stock Temp");
        legendText.setPosition(RectangleEdge.BOTTOM);
        legendText.setHorizontalAlignment(HorizontalAlignment.LEFT);
        chart.addSubtitle(legendText);
        legendText = new TextTitle("Test Conditions :");
        legendText.setPosition(RectangleEdge.TOP);
        chart.addSubtitle(legendText);
        NumberAxis xAxis = (NumberAxis) plot.getDomainAxis(0);
        xAxis.setTickUnit(new NumberTickUnit(50));

        plot.getRendererForDataset(plot.getDataset(0)).setSeriesPaint(0,
                Color.red);
        plot.getRendererForDataset(plot.getDataset(0)).setSeriesPaint(1,
                Color.blue);
        plot.getRendererForDataset(plot.getDataset(0)).setSeriesPaint(2,
                Color.green);
        plot.getRendererForDataset(plot.getDataset(0)).setSeriesPaint(3,
                Color.black);
        plot.getRendererForDataset(plot.getDataset(0)).setSeriesPaint(4,
                Color.black);
        plot.getRendererForDataset(plot.getDataset(0)).setSeriesPaint(5,
                Color.black);
        plot.getRendererForDataset(plot.getDataset(0)).setSeriesPaint(6,
                Color.black);
    }
}

文件打开.java

public class FileOpen extends JPanel {

    public static XYSeries series1 = new XYSeries("Temperature");
    public static XYSeries series2 = new XYSeries("Torque");
    public static XYSeries series3 = new XYSeries("Speed");
    public static XYSeries ASeries = new XYSeries("A");
    public static XYSeries BSeries = new XYSeries("B");
    public static XYSeries XSeries = new XYSeries("X");
    public static XYSeries DSeries = new XYSeries("D");
    public static XYSeriesCollection dataset;
    public static int temp[] = new int[2000];
    public static int torque[] = new int[2000];
    public static int speed[] = new int[2000];
    public static int times[] = new int[2000];

    private static final long serialVersionUID = 1L;
    final JFileChooser fc = new JFileChooser();

    public static int atorque = 0, btorque = 0, xtorque = 0, dtorque = 0,
            atemp = 0, btemp = 0, xtemp = 0, dtemp = 0;
    public static int a = 0, b = 0, x = 0, d = 0;

    SplineChart myChart;

    public FileOpen() throws Exception {
        getFile();
    }

    public void getFile() throws Exception {
        fc.addChoosableFileFilter(new CSVFilter());
        int result = fc.showOpenDialog(this);
        if (result == JFileChooser.APPROVE_OPTION) {
            Thread thread = new Thread(new Runnable() {
                public void run() {
                    try {
                        process(fc.getSelectedFile());
                    }

                    catch (Exception e) {

                    }
                    finally {

                    }
                }
            });
            thread.start();
        } else {
            System.exit(0);
        }
    }

    public void process(File file) throws FileNotFoundException, Exception {

        CSVReader reader = new CSVReader(new InputStreamReader(
                new FileInputStream(file.getAbsolutePath()), "UTF-16LE"), '\t',
                '\'', 1);

        int i = 0;
        try {
            FileInputStream fstream = new FileInputStream(file);

            BufferedReader br = new BufferedReader(new InputStreamReader(fstream,
                    "UTF-16LE"));
            String strLine;
            br.readLine();
            br.readLine();
            reader.readNext();
            reader.readNext();

            int count = 0;
            while ((strLine = br.readLine()) != null) {
                String values[] = strLine.split("\t");
                try {

                    for (int j = 0; j < values.length; j++)
                        System.out.println("" + j + values[j]);

                    if (i > 242)
                        break;
                    times[i] = i;
                    temp[i] = Integer.parseInt(values[3]);
                    torque[i] = Integer.parseInt(values[4]);
                    speed[i] = Integer.parseInt(values[5]);

                } catch (NumberFormatException e) {
                    System.out.println("NFE");
                }

                System.out.println(times[i] + "," + temp[i] + "," + torque[i]
                        + "," + speed[i]);
                series1.add(i, temp[i]);
                series2.add(i, torque[i]);
                series3.add(i, speed[i]);
                i++;
            }

            // Close the input stream
            in.close();

            for (int j = 0; j < torque.length; j++) {
                if (atorque < torque[j]) {
                    a = j;
                    atorque = torque[j];
                    atemp = temp[j];
                }
            }
            btorque = atorque;
            for (int j = (a); j < torque.length; j++) {
                if (btorque > torque[j] && torque[j] != 0) {
                    b = j;
                    btorque = torque[j];
                    btemp = temp[j];
                }
            }
            int pxtorque = 0, slope = 0;
            for (int j = (b + 1); j < torque.length; j++) {
                slope = (xtorque - pxtorque);
                if ((torque[j] - torque[j - 1]) < 0) {
                    break;
                }
                if (xtorque < torque[j]) {
                    x = j;
                    xtorque = torque[j];
                    xtemp = temp[j];

                    System.out.println(slope + "" + b);
                }
                // pxtorque = xtorque;
            }

            for (int j = (x); j < torque.length; j++) {
                int counter = 0;
                for (int k = 0; k < 10; k++)
                    if (torque[j + counter] == torque[j + 1 + counter])
                        counter++;

                if (counter == 10) { // straight line
                    if ((torque[j + 1 + counter] - torque[j + counter]) == 0) {
                    } else {
                        d = j + counter;
                        dtorque = torque[j + counter];
                        dtemp = temp[j + counter];
                        break;
                    }
                }
            }
            System.out.println(a + " " + b + " " + x + " " + d);
        } catch (Exception e) {
            e.printStackTrace();
        }

        myChart = new SplineChart("Master Blend", "Demo", updateDataset());
        myChart.setExtendedState(JFrame.MAXIMIZED_BOTH);
        myChart.setVisible(true);
    }

    public static XYSeriesCollection updateDataset() {
        dataset = new XYSeriesCollection();
        ASeries = new XYSeries("A");
        BSeries = new XYSeries("B");
        XSeries = new XYSeries("X");
        DSeries = new XYSeries("D");
        ASeries.add(a, atorque);
        ASeries.add(a, 0);
        BSeries.add(b, btorque);
        BSeries.add(b, 0);
        XSeries.add(x, xtorque);
        XSeries.add(x, 0);
        DSeries.add(d, dtorque);
        DSeries.add(d, 0);
        dataset.addSeries(series1);
        dataset.addSeries(series2);
        dataset.addSeries(series3);
        dataset.addSeries(ASeries);
        dataset.addSeries(BSeries);
        dataset.addSeries(XSeries);
        dataset.addSeries(DSeries);
        return dataset;
    }
}
4

0 回答 0