使用java动态地根据月份绘制BarGraph [英] Plotting BarGraph according to month days dynamically using java

查看:127
本文介绍了使用java动态地根据月份绘制BarGraph的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在java中使用 jfreechart 创建了 BarChart 。酒吧根据一个月的天数绘制。例如本月3月包含31天,所以将有31个酒吧。问题是当我点击下一个按钮时,月份变为4月,其中包含30天,即30个小节。如何根据点击按钮的一个月内的日期更改 BarChart

i've created a BarChart using jfreechart in java. The bar is plotted according to the days in a month. for example this month March contains 31 days, so there will be 31 bars . The problem is that when i click the next button the month changes to April which contains 30 days, ie 30 bars. How can we change the BarChart according to the days in a month on button click.

任何人都可以告诉我怎么做。

Can anyone please tell me how to do that.

推荐答案

您需要更新数据集每次改变。我添加了一个 updateDataset()方法并在几个关键位置调用它。

You need to update your dataset with each change. I've added an updateDataset() method and called it in several key places.

private void updateDataset() {
    dataset.clear();
    for (int i = 1; i <= finalday; i++) {
        dataset.setValue(i, "Marks", "" + i);
    }

注:


  • 使用绝对布局;让布局完成它的工作。

  • Do not use absolute layout; let the layout do its work.

自己做日期算术;使用日历,用于示例

Do not do date arithmetic yourself; use Calendar, for example.

在构造函数中调用可覆盖的方法。

Do not call overridable methods in the constructor.

重新计算你的因素用于限制变量数量和范围的代码。

Do re-factor your code to limit the number and scope of variables.

使用有意义的名称,尤其是实例变量。

Do use meaningful names, especially for instance variables.

SSCCE,未完全修订:

SSCCE, incompletely revised:

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
import java.util.Calendar;
import java.util.GregorianCalendar;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.data.category.DefaultCategoryDataset;

public class BarGraph {

    public int count = 0, count1 = 0, count2 = 0, count3 = 0, count4 = 0;
    public int contstatus = 2;
    public int date, year, mon, fn, show = 0, finalday, leapcn = 0, task = 0;
    String startdte, enddte, monthweek, leavedates = "", nneed = "", year4enab, month4enab, days;
    ChartFrame frame;
    public static int st = 0;
    JButton left = new JButton("<");
    JButton right = new JButton(">");
    JComboBox month = new JComboBox();
    JSpinner yearspin = new javax.swing.JSpinner();
    JLabel monthLabel = new javax.swing.JLabel();
    JLabel yearLabel = new javax.swing.JLabel();
    DefaultCategoryDataset dataset = new DefaultCategoryDataset();

    BarGraph() {
        task = 1;
        Calendar cal;

        left.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        left.setRequestFocusEnabled(false);
        left.addActionListener(new java.awt.event.ActionListener() {
            @Override
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                leftActionPerformed(evt);
            }
        });


        right.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        right.setRequestFocusEnabled(false);
        right.addActionListener(new java.awt.event.ActionListener() {
            @Override
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rightActionPerformed(evt);
            }
        });

        month.setMaximumRowCount(12);
        month.setModel(new javax.swing.DefaultComboBoxModel(new String[]{"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}));
        month.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        month.addItemListener(new java.awt.event.ItemListener() {
            @Override
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                monthItemStateChanged(evt);
            }
        });

        yearspin.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        yearspin.setValue(year);
        yearspin.addChangeListener(new javax.swing.event.ChangeListener() {
            @Override
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                yearspinStateChanged(evt);
            }
        });

        monthLabel.setBackground(new java.awt.Color(255, 255, 255));
        monthLabel.setFont(new java.awt.Font("Tahoma", 1, 13)); // NOI18N
        monthLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        monthLabel.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED, null, java.awt.Color.black, null, null));
        monthLabel.setOpaque(true);
        monthLabel.setText("SEPTEMBER");

        yearLabel.setBackground(new java.awt.Color(255, 255, 255));
        yearLabel.setFont(new java.awt.Font("Tahoma", 1, 13)); // NOI18N
        yearLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        yearLabel.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED, null, java.awt.Color.black, null, null));
        yearLabel.setOpaque(true);
        yearLabel.setText("2013");
        cal = new GregorianCalendar();
        year = cal.get(Calendar.YEAR);
        mon = cal.get(Calendar.MONTH);
        date = cal.get(Calendar.DATE);
        yearspin.setValue(year);
        month.setSelectedIndex(mon);
        calendar(year, mon);
        month4enab = getMonth(monthLabel.getText());
        year4enab = yearLabel.getText();

        startdte = year4enab + "-" + month4enab + "-01";
        enddte = year4enab + "-" + month4enab + "-" + finalday;
    }

    public static void main(String[] args) {
        new BarGraph().showBar();

    }

    private void leftActionPerformed(ActionEvent evt) {
        show = 0;
        if (mon == 0) {
            year--;
            mon = 11;
        } else {
            mon--;
        }
        yearspin.setValue(year);
        month.setSelectedIndex(mon);
        display();
        calendar(year, mon);
        displayCalendar();
        updateDataset();
    }

    private void rightActionPerformed(ActionEvent evt) {
        show = 0;
        if (mon == 11) {
            year++;
            mon = 0;
        } else {
            mon++;
        }
        yearspin.setValue(year);
        month.setSelectedIndex(mon);
        display();
        calendar(year, mon);
        displayCalendar();
        updateDataset();
    }

    private void displayCalendar() {
        month4enab = getMonth(monthLabel.getText());
        year4enab = yearLabel.getText();
        startdte = year4enab + "-" + month4enab + "-01";
        enddte = year4enab + "-" + month4enab + "-" + finalday;
    }

    private void monthItemStateChanged(ItemEvent evt) {
        show = 0;
        String s = (String) month.getSelectedItem();

        if ("January".equals(s)) {
            mon = 0;
        }
        if ("February".equals(s)) {
            mon = 1;
        }
        if ("March".equals(s)) {
            mon = 2;
        }
        if ("April".equals(s)) {
            mon = 3;
        }
        if ("May".equals(s)) {
            mon = 4;
        }
        if ("June".equals(s)) {
            mon = 5;
        }
        if ("July".equals(s)) {
            mon = 6;
        }
        if ("August".equals(s)) {
            mon = 7;
        }
        if ("September".equals(s)) {
            mon = 8;
        }
        if ("October".equals(s)) {
            mon = 9;
        }
        if ("November".equals(s)) {
            mon = 10;
        }
        if ("December".equals(s)) {
            mon = 11;
        }
        display();
        calendar(year, mon);
        updateDataset();
    }

    private void yearspinStateChanged(ChangeEvent evt) {
        show = 0;
        year = (Integer) yearspin.getValue();
        display();
        calendar(year, mon);
        updateDataset();
    }

    public void display() {
        String month = "", day = "";
        if (mon < 9) {
            month = "0" + (mon + 1);
        } else {
            month = "" + (mon + 1);
        }
        if (nneed.length() < 2) {
            day = "0" + nneed;
        } else {
            day = "" + nneed;
        }

        String datss = year + "-" + (month) + "-" + day;
        switch (mon) {
            case 0:
                monthLabel.setText("JANUARY");
                break;
            case 1:
                monthLabel.setText("FEBRUARY");
                break;
            case 2:
                monthLabel.setText("MARCH");
                break;
            case 3:
                monthLabel.setText("APRIL");
                break;
            case 4:
                monthLabel.setText("MAY");
                break;
            case 5:
                monthLabel.setText("JUNE");
                break;
            case 6:
                monthLabel.setText("JULY");
                break;
            case 7:
                monthLabel.setText("AUGUST");
                break;
            case 8:
                monthLabel.setText("SEPTEMBER");
                break;
            case 9:
                monthLabel.setText("OCTOBER");
                break;
            case 10:
                monthLabel.setText("NOVEMBER");
                break;
            case 11:
                monthLabel.setText("DECEMBER");
                break;

        }
        yearLabel.setText(String.valueOf(year));
    }

    public void calendar(int year, int mon) {
        int year1 = year;
        int count1 = 1, fun = 0, day, day1 = 0, key = 0, k = -1, s = 1, ck = 2, cheak, y = 0;
        day1 = year % 100;
        fun = 1 + (day1 / 4);
        switch (mon) {
            case 0:
                key = 1;
                k = -1;
                break;
            case 1:
                key = 4;
                k = 0;
                break;
            case 2:
                key = 4;
                k = 0;
                break;
            case 3:
                key = 0;
                k = 0;
                break;
            case 4:
                key = 2;
                k = 0;
                break;
            case 5:
                key = 5;
                k = 0;
                break;
            case 6:
                key = 0;
                k = 0;
                break;
            case 7:
                key = 3;
                k = 0;
                break;
            case 8:
                key = 6;
                k = 0;
                break;
            case 9:
                key = 1;
                k = 0;
                break;
            case 10:
                key = 4;
                k = 0;
                break;
            case 11:
                key = 6;
                k = 0;
                break;
        }

        if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
            k = -1;
            day = 29;
            leapcn = 0;
            if (mon == 0 || mon == 1) {
                leapcn = 1;
            }
        } else {
            k = 0;
            day = 28;
            leapcn = 1;
        }

        if (year >= 2000) {
            y = 6;
        }
        if (year <= 1900 && year > 2000) {
            y = 0;

        }
        fun = fun + key + k + y;
        fun = (fun + day1) % 7;
        if (fun == 0) {
            fun = 7;
        }
        if (mon == 0 || mon == 2 || mon == 4 || mon == 6 || mon == 7 || mon == 9 || mon == 11) {
            day = 31;
        }
        if (mon == 3 || mon == 5 || mon == 8 || mon == 10) {
            day = 30;
        }
        finalday = day;
        System.out.println("DAYS IN THIS MONTHS:" + finalday);
        cheak = fun + 1;
        s++;
    }

    public static String getMonth(String s) {
        String mont = "56";
        if ("January".equalsIgnoreCase(s)) {
            mont = "01";
        } else if ("February".equalsIgnoreCase(s)) {
            mont = "02";
        } else if ("March".equalsIgnoreCase(s)) {
            mont = "03";
        } else if ("April".equalsIgnoreCase(s)) {
            mont = "04";
        } else if ("May".equalsIgnoreCase(s)) {
            mont = "05";
        } else if ("June".equalsIgnoreCase(s)) {
            mont = "06";
        } else if ("July".equalsIgnoreCase(s)) {
            mont = "07";
        } else if ("August".equalsIgnoreCase(s)) {
            mont = "08";
        } else if ("September".equalsIgnoreCase(s)) {
            mont = "09";
        } else if ("October".equalsIgnoreCase(s)) {
            mont = "10";
        } else if ("November".equalsIgnoreCase(s)) {
            mont = "11";
        } else if ("December".equalsIgnoreCase(s)) {
            mont = "12";
        }
        return mont;
    }

    private void showBar() {
        for (int i = 1; i <= finalday; i++) {
            dataset.setValue(i, "Marks", "" + i);
        }
        JFreeChart chart = ChartFactory.createBarChart(" ", "Student", "Marks",
            dataset, PlotOrientation.VERTICAL, false, true, false);
        chart.setBackgroundPaint(Color.WHITE);

        CategoryPlot plot = (CategoryPlot) chart.getPlot();
        plot.setBackgroundPaint(new Color(221, 223, 238));
        plot.setRangeGridlinePaint(Color.white);
        BarRenderer renderer = (BarRenderer) plot.getRenderer();
        renderer.setSeriesPaint(0, new Color(231, 175, 61));
        renderer.setSeriesPaint(1, Color.green);
        renderer.setDrawBarOutline(false);
        renderer.setShadowVisible(false);
        chart.setBackgroundPaint(Color.WHITE);
        chart.getTitle().setPaint(Color.blue);
        CategoryPlot p = chart.getCategoryPlot();
        p.setRangeGridlinePaint(Color.BLUE);

        frame = new ChartFrame("Bar Chart", chart);
        frame.add(month);
        frame.add(left);
        frame.add(right);
        frame.add(yearspin);
        frame.add(monthLabel);
        frame.add(yearLabel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

    }

    private void updateDataset() {
        dataset.clear();
        for (int i = 1; i <= finalday; i++) {
            dataset.setValue(i, "Marks", "" + i);
        }
    }
}

这篇关于使用java动态地根据月份绘制BarGraph的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆