更新数据时,Javafx条形图X轴无法正确缩放

时间:2018-05-30 01:39:34

标签: java javafx bar-chart

我正在尝试使用Javafx实现条形图。如果我只绘制一次数据,那么图表工作正常。但是当我更新数据并重新绘制图表时,X轴刻度保持不变,而所有数据都正确更新。请检查以下图像。

这是按周计算数据的正确图表

data by week

当我更新数据时,图表变为

data by month

您可以看到数据已更新,但X轴无法正确缩放。有趣的是,当我在不改变数据的情况下再次绘制图表时,X轴就像这样变得正常。

data by month2

代码如下:

private static void drawChart(int timeMode)
{
    stayAvg = new XYChart.Series<String, Number>();
    stayAvg.setName("Avg.Stay (mins)");

    // add data to the dataset
    if (timeMode == 1)
    {
        for (int i = firstWeek; i <= lastWeek; i++)
        {
            stayAvg.getData().add(new XYChart.Data<String, Number>(weeks[i], weeksTotalCount[i]));
        }
    }
    else if (timeMode == 2)
    {
        for (int i = firstMonth; i <= lastMonth; i++)
        {
            stayAvg.getData().add(new XYChart.Data<String, Number>(months[i], monthsTotalCount[i]));
        }
    }

    // display chart
    Platform.runLater(new Runnable() {
        @Override
        public void run() {
            // plot data sets to the chart
            // Update UI here.
            // set up tooltips when cursor hovers on bars
            sbc.setData(FXCollections.observableArrayList(stayAvg));
            stayAvg.getData().forEach(d -> {
                    Tooltip tip = new Tooltip();
                    tip.setText(d.getYValue() + "\n" + d.getXValue());
                    Tooltip.install(d.getNode(), tip);
            });
            // sbc is a StackedBarChart
            sbc.setTitle("Average Stay");
            chart.setScene(scene);
        }
    });
}

3 个答案:

答案 0 :(得分:1)

首先,启用图表动画。最后,禁用聊天动画。

    lineChart.getData().clear();
    lineChart.setAnimated(true);// enable animation
    XYChart.Series series = new XYChart.Series();

    String query = "SELECT time, total_purchase FROM sales WHERE date = '"+ 
    text.getText() +"'";

    con = new Connector().connector();
    pst = con.prepareStatement(query);
    ResultSet rs = pst.executeQuery();
    rs.beforeFirst();

    while(rs.next()){

        String time = rs.getString(rs.findColumn("time"));
        float purchase = rs.getFloat(rs.findColumn("total_purchase"));

        series.getData().add(new XYChart.Data(time, purchase));
    }
    lineChart.getData().addAll(series);
    lineChart.setAnimated(false);// disable animation

答案 1 :(得分:0)

你每次画画时都尝试过不设置数据:

 sbc.setData(FXCollections.observableArrayList(stayAvg));

而是做:

private static void drawChart(int timeMode)
{
 sbc.getData().clear();

// add data to the dataset
if (timeMode == 1)
{
    for (int i = firstWeek; i <= lastWeek; i++)
    {
        stayAvg.getData().add(new XYChart.Data<String, Number>(weeks[i], weeksTotalCount[i]));
    }
}
else if (timeMode == 2)
{
    for (int i = firstMonth; i <= lastMonth; i++)
    {
        stayAvg.getData().add(new XYChart.Data<String, Number>(months[i], monthsTotalCount[i]));
    }
}

答案 2 :(得分:0)

您可以尝试禁用条形图的动画:

chart.setAnimated(false);

这将防止条形图更新之间看似“平滑”的过渡,从而防止出现那些意外的视觉错误,例如条形标签过度拥挤。

这受this问题的启发,在我的情况下有效。