【问题标题】:Flot chart won't re-draw浮动图表不会重新绘制
【发布时间】:2014-02-10 22:44:17
【问题描述】:

当页面最初加载时,我得到一组数据,并将其显示在浮动图表中。我有一个下拉菜单,允许用户选择“年初至今”或“月初至今”,一切正常,除了图表没有重绘图表,所以当您将选择更改为“月到日期”,它仍然在 x 轴上列出所有日历月,而不是当月的日期。我试过使用$.plot.setupGrid() 和$.plot.draw(),还有chartTaggedSpending.setupGrid() 和chartTaggedSpending.draw(),但它不起作用,所以我在这里做错了。提前感谢您提供的任何帮助。

这是ajax.done()功能块中调用的代码:

if(selectView = 1 && response.length != 0) {

        var chartTaggedSpending = $('#tagged-spending');
        var arrMonthLabels = [[1, 'Jan'], [2, 'Feb'], [3, 'Mar'],[4, 'Apr'],
[5, 'May'],[6, 'Jun'], [7, 'Jul'], [8, 'Aug'], [9, 'Sep'], [10, 'Oct'],
[11, 'Nov'], [12, 'Dec']];
        $.plot(chartTaggedSpending,
            [
                {
                    data: response[1].data,
                    label: 'Budgeted: ' + response[1].label,
                    lines: { show: true},
                    points: { show: true, radius: 7 }
                },
                {
                    data: response[0].data,
                    label: 'Spent: ' + response[0].label,
                    lines: { show: true}, 
                    points: { show: true, radius: 7 }
                }
            ],
            {
                colors: ['#a6db34', '#33a6db'],   
                legend: { show: true },
                grid: { borderWidth: 0, hoverable: true, clickable: true },
                yaxis: {min: 0, max: 2000, ticks: 10, tickColor: '#eeeeee' },
                xaxis: { 
                    min: 1,
                    max: 12,
                    ticks: arrMonthLabels,
                    tickColor: '#ffffff' 
                }
            }
        );
    }
    if(selectView = 0 && response.length != 0) {
        var chartTaggedSpending = $('#tagged-spending');
        $.plot(chartTaggedSpending,
            [
                {
                    data: response[1].data,
                    label: 'Budgeted: ' + response[1].label,
                    lines: { show: true},  
                    points: { show: true, radius: 7 },
                },
                {
                    data: response[0].data,
                    label: 'Spent: ' + response[0].label,
                    lines: { show: true}, 
                    points: { show: true, radius: 7 }
                }
            ],
            {
                colors: ['#a6db34', '#33a6db'],   
                legend: { show: true },
                grid: { borderWidth: 0, hoverable: true, clickable: true },
                yaxis: {min: 0, max: 1500, ticks: 10, tickColor: '#eeeeee' },
                xaxis: { 
                    min: 1,
                    max: 31,
                    ticks: 31,
                    tickColor: '#ffffff' 
                }
            }
        );
    }

【问题讨论】:

  • 你能告诉我你的问题的解决方案吗?其实我也有同样的问题。

标签: jquery ajax flot


【解决方案1】:

.setupGrid 和 .draw 是绘图对象的方法。此绘图对象从.plot 调用返回。

var chartTaggedSpending = $('#tagged-spending');
var myPlot = $.plot(chartTaggedSpending, etc.... // assign the return from $.plot
// sometime later
myPlot.setupGrid(); // use it later
myPlot.draw();

【讨论】:

  • 我尝试像您在此处演示的那样构建上面的代码,但我一定遗漏了一些东西,因为它仍然无法正常工作。当我将选择选项更改为另一个图表时,数据会发生变化,因此我知道在那之前一切正常,但无论我如何尝试,它都不会重绘图表。
  • @hyphen,你能创建一个 jsFiddle 来重现你的问题吗?
猜你喜欢
  • 2011-12-22
  • 1970-01-01
  • 1970-01-01
  • 2023-04-10
  • 1970-01-01
  • 2014-01-10
  • 1970-01-01
  • 2015-09-23
  • 1970-01-01
相关资源
最近更新 更多