【问题标题】:Having problems with jqPlot bar chartjqPlot条形图有问题
【发布时间】:2013-02-26 11:19:51
【问题描述】:

我正在使用 jqPlot 创建条形图,但遇到了一些问题。

问题 1: 图表上的第一个和最后一个条被截断。只显示了一半

问题 2:我不希望我的数据点跨越整个 x 轴。有没有数据跨越整个x轴?

ex: 这就是现在所做的。

这是我传递给它的数据

var chartData = [["19-Jan-2012",2.61],["20-Jan-2012",5.00],["21-Jan-2012",6.00]]

这是我正在使用的 jquery。

 // Plot chart
        function PlotChart(chartData, numberOfTicks) {
            $.jqplot.config.enablePlugins = true;
                 var plot2 = $.jqplot('chart1', [chartData], {
                    title: 'Mouse Cursor Tracking',
                     seriesDefaults:{
                         renderer: $.jqplot.BarRenderer,
                         rendererOptions: {
                            barPadding: 1,
                            barMargin: 15,
                            barDirection: 'vertical',
                            barWidth: 50
                        }, 
                        pointLabels: { show: true }
                    },
                    axes: {
                        xaxis: {
                            pad: 0,       // a factor multiplied by the data range on the axis to give the
                            numberTicks: numberOfTicks,
                            renderer:  $.jqplot.DateAxisRenderer,  // renderer to use to draw the axis,
                            tickOptions: {
                                formatString: '%b %#d'   // format string to use with the axis tick formatter
                            }
                        },
                        yaxis: {
                            tickOptions: {
                                formatString: '$%.2f'
                            }
                        }
                    },
                    highlighter: {
                        sizeAdjust: 7.5
                    },
                    cursor: {
                        show: true
                    }
                });
            }

【问题讨论】:

  • 改变你的 xaxis 垫,改变或不硬核滴答数。
  • 我尝试更换垫子,但没有任何效果。刻度数设置为要显示的项目数...我会尝试增加它
  • 增加项目数量没有影响

标签: jquery jquery-plugins jqplot


【解决方案1】:

从您希望情节的外观来看,如果您改用 CategoryAxisRenderer 而不是 DateAxisRenderer,您会省去很多麻烦。与真正的时间序列相比,CategoryAxisRenderer 在显示数据的谨慎分组方面要好得多。

var axisDates = ["Jan 19", "Jan 20", "Jan 21"]
var chartData = [2.61,5.00,6.00]

        $.jqplot.config.enablePlugins = true;
             var plot2 = $.jqplot('chart2', [chartData], {
                title: 'Some Plot',
                 seriesDefaults:{
                     renderer: $.jqplot.BarRenderer,
                     rendererOptions: {
                        barPadding: 1,
                        barMargin: 15,
                        barDirection: 'vertical',
                        barWidth: 50
                    }, 
                    pointLabels: { show: true }
                },
                axes: {
                    xaxis: {                            
                            renderer:  $.jqplot.CategoryAxisRenderer,
                            ticks: axisDates
                    },
                    yaxis: {
                        tickOptions: {
                            formatString: '$%.2f'
                        }
                    }
                },
                highlighter: {
                    sizeAdjust: 7.5
                },
                cursor: {
                    show: true
                }
            });

【讨论】:

    【解决方案2】:

    DateAxisRenderer 真正适用于折线图,而不是条形图。当您将它与条形图结合使用时,它就无法正常工作。 DateAxisRenderer 的想法/目标是在日期/时间上制作线性/准确的时间图。这样,如果您错过了日期条目,它仍然会随着时间的推移按比例绘制。在此处查看他们在 DateAxisRenderer 上的示例:http://www.jqplot.com/tests/date-axes.php

    您想要使用的是 CategoryAxisRenderer。然后,您可以覆盖/创建自己的刻度标签渲染器,一切顺利。通常,您不希望将额外的空项目附加到项目,尤其是当它们为空时,但是,如果您这样做,只需将它们附加到您的数据数组。

    这是一个 jsfiddle 做你想做的事:http://jsfiddle.net/fordlover49/JWhmQ/

    请注意,您可能需要查看管理资源部分以验证您需要引用哪些文件(除了 jquery 文件)。

    这是 jsfiddle 发生故障时的 javascript:

    $.jqplot.config.enablePlugins = true;
    var chartData = [["19-Jan-2012", 2.61], ["20-Jan-2012", 5.00], ["21-Jan-2012", 6.00]];
    
    // add a custom tick formatter, so that you don't have to include the entire date renderer library.
    $.jqplot.DateTickFormatter = function(format, val) {
        // for some reason, format isn't being passed through properly, so just going to hard code for purpose of this jsfiddle
        val = (new Date(val)).getTime();
        format = '%b&nbsp%#d'
        return $.jsDate.strftime(val, format);
    };
    
    function PlotChart(chartData, extraDays) {
        // if you want extra days, just append them to your chartData array.
        if (typeof extraDays === "number") {
            for (var i = 0; i < extraDays; i++) {
                var actualDate = new Date(chartData[chartData.length - 1]); // convert last entry to actual date
                var newDate = new Date(actualDate.getFullYear(), actualDate.getMonth(), actualDate.getDate() + 1); // create new increased date
                chartData.push([newDate, 0]);
            }
        }
    
        var plot2 = $.jqplot('chart1', [chartData], {
            title: 'Mouse Cursor Tracking',
            seriesDefaults: {
                renderer: $.jqplot.BarRenderer,
                rendererOptions: {
                    barPadding: 1,
                    barWidth: 50
                },
                pointLabels: {
                    show: true
                }
            },
            axes: {
                xaxis: {
                    pad: 1,
                    // a factor multiplied by the data range on the axis to give the            
                    renderer: $.jqplot.CategoryAxisRenderer,
                    // renderer to use to draw the axis,     
                    tickOptions: {
                        formatString: '%b %#d',
                        formatter: $.jqplot.DateTickFormatter
                    }
                },
                yaxis: {
                    tickOptions: {
                        formatString: '$%.2f'
                    }
                }
            },
            highlighter: {
                sizeAdjust: 7.5
            },
            cursor: {
                show: true
            }
        });
    }
    
    PlotChart(chartData, 3);
    

    【讨论】:

      【解决方案3】:

      这是我用来显示页边距的简单技巧。

      我创建了一个开始日期和一个结束日期,分别是我要显示的内容的前一天和后一天。

      例如,如果我想显示 2012 年 1 月的月份

      var startingDate = new Date(2012, 0, 0); //=> 31th Dec 2011
      var endingDate = new Date(2012, 1, 1); //=> 1st Feb 2012
      

      然后我声明我自己的DateTickFormatter 我不会打印出这两个日期。

      $.jqplot.DateTickFormatter = function(format, val) {
              if (!format) {
                  format = '%Y/%m/%d';
              }
      
              if(val==startingDate.getTime() || val==endingDate.getTime()){
                  return "";
              }else{
                  return $.jsDate.strftime(val, format);
              }
          };
      

      然后在xaxis我放了以下选项:

      xaxis : {
            renderer:$.jqplot.DateAxisRenderer, 
            min:startingDate,
            max:endingDate,
            tickInterval:'1 day'
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-10-16
        • 2015-01-04
        • 2012-05-11
        • 2012-09-29
        相关资源
        最近更新 更多