【问题标题】:jqplot bar graph issuejqplot条形图问题
【发布时间】:2013-03-12 14:49:44
【问题描述】:

我需要帮助来使用 jqplot 绘制图形。该图很简单,但 jqplot 使它变得复杂。 我需要得到这样的图表:

.

有一些改进:

颜色在这里定义:

graphColors = ['#048ff1', '#79b924', '#ffa600', '#ef5257', '#7b279b', '#8ff104',
               '#b92479', '#5257ef', '#279b7b', '#f1048f', '#00ffa6', '#9b7b27']

这里有一些渲染选项:

seriesDefaults: {
    seriesColors: graphColors,
    renderer: $.jqplot.BarRenderer,
    rendererOptions: { barDirection: 'vertical' }
    },
    axes: {
        yaxis: {
            renderer: $.jqplot.CategoryAxisRenderer,
            ticks: [ /* to be filled in automatically */ ]
        },
    xaxis: {
        min: 0
        }
    }

我需要渲染的数据在这里:

//in the image I used instead of letters '1'
data = [[['a', 1112]],
        [['b', 1127]],
        [['c', 822]],
        [['d', 1039]]
       ];

问题:

  1. 如何为每个条设置一个标签('a'、'b'、'c'、'd'等)?

  2. 如何将系列移动到从左侧开始(而不是像现在这样在中间,当然左侧有一个小边距?

  3. y 轴上的值,大于 1000 的值会在图形线上呈现。如何在坐标轴值和图形之间设置空格?

  4. 我想画的图很简单。不使用系列可以获得相同的结果吗?我想要的只是一个条形图,每个条形都有不同的颜色并显示特定的标签?

谢谢。

【问题讨论】:

  • 你说它是一个如此简单的图表让我很开心,但是你需要一个非常具体的调整列表。在上面的数字 2 中,如果图表没有居中,您是否希望右侧有很多空白区域?
  • 应该很简单。我无法想象比条形图更简单的图表,其中每个条形都是一对 [标签,值]。
  • 我需要所有的图表左对齐。条的数量可能会有所不同。在 2 个条之间,我需要定义距离(现在假设为 30px)。也应该是左填充 - 第一个栏和 y 轴之间的距离。

标签: javascript jqplot


【解决方案1】:

请参阅下面的代码和 cmets。小提琴here.

您列表中的第 2 项,我不清楚,如果您将情节推到左侧,则右侧有空白区域。最好让情节填满所有可用空间。

$(document).ready(function(){

   var graphColors = ['#048ff1', '#79b924', '#ffa600', '#ef5257', '#7b279b', '#8ff104',
           '#b92479', '#5257ef', '#279b7b', '#f1048f', '#00ffa6', '#9b7b27'];

   var data = [
        [1112],
        [1127],    
        [822],
        [1039]];

    var ticks = ['This is how to tick'];

    plot2 = $.jqplot('chart1', data, {
        seriesColors: graphColors, 
        seriesDefaults: {                
            renderer:$.jqplot.BarRenderer
        },
        axesDefaults:{pad: 1.3}, // Item 2, increase this padding so labels are cut off
        // Item 4, no, jqplot treats each differently colored bar as a series, so you must provide series options
        series:[
            {pointLabels:{show:true,labels:['a']}}, // Item 1, the label for each bar is the "labels" array 
            {pointLabels:{show:true,labels:['b']}},
            {pointLabels:{show:true,labels:['c']}},
            {pointLabels:{show:true,labels:['d']}}
        ],
        axes: {                
            xaxis: {
                renderer: $.jqplot.CategoryAxisRenderer,
                ticks: ticks // comment this out for "autoticks"
            }
        }
    });
});

【讨论】:

  • 感谢您的回答。关于第二个问题:我需要将图形向左对齐,即使这意味着右侧有很多可用空间。每个条应具有固定的大小(宽度),并且 2 个条之间应有固定的距离。剩下的空间仍然是空的。
  • 您知道如何从字符串数组中动态设置 pointLabels 值吗?我在一个单独的函数中构建该部分,并将它作为参数传递给 $.jqplot 函数。我有一个单独的函数,它使用一些参数为我构建 jqplot 选项。
  • 类似series = $.map(['a','b','c','d'], function(i){return({pointLabels:{show:true,labels:[i]}})})?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-16
  • 2015-01-04
  • 2012-09-29
  • 2012-03-05
相关资源
最近更新 更多