jqPlot 条形图出现问题
我正在使用 jqPlot 创建条形图,但遇到了一些问题。
问题 1: 图表上的第一个和最后一个条形图被截断。只有一半显示
问题 2:我不希望我的数据点跨越整个 x 轴。是否不能让数据跨越整个 x 轴?
例如:这就是现在所做的。
这是我传递给它的数据
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
}
});
}
I'm using jqPlot to create a bar graph, but I ran into a few problems.
Problem 1: The first and last bars on the graph are cut off. Only half of it is displaying
Problem 2: I don't want my data points to span the entire x-axis. Is there to not have the data span the entire x-axis?
ex: This is what is does right now.
This is the data I am passing into it
var chartData = [["19-Jan-2012",2.61],["20-Jan-2012",5.00],["21-Jan-2012",6.00]]
This is the jquery I am using.
// 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
}
});
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
从您希望绘图的外观来看,如果您改用 CategoryAxisRenderer 而不是 DateAxisRenderer,将为您省去很多麻烦。与真实的时间序列相比,CategoryAxisRenderer 更擅长显示离散的数据分组。
From how you want your plot to look, you'll save yourself a lot of trouble if you switch to using a CategoryAxisRenderer instead of the DateAxisRenderer. The CategoryAxisRenderer is a lot better at displaying discreet groupings of data as opposed to a true time series.
DateAxisRenderer 实际上适用于折线图,而不是条形图。当你将它与条形图结合起来时,它就无法正常工作。 DateAxisRenderer 的想法/目标是在日期/时间上制作线性/准确的时间图。这样,如果您错过了日期输入,它仍然会随着时间的推移按比例绘制。在此处检查 DateAxisRenderer 上的示例: http://www.jqplot.com/tests/date -axes.php
您想要使用的是 CategoryAxisRenderer。然后,您可以覆盖/创建您自己的刻度标签渲染器,然后就可以开始了。通常,您不想将额外的空项目附加到项目中,尤其是当它们为空时,但是,如果您这样做,只需将它们附加到数据数组中即可。
这是一个 jsfiddle 做你想做的事: http://jsfiddle.net/fordlover49/JWhmQ/
请注意您可能需要查看管理资源部分来验证您需要引用哪些文件(除了 jquery 文件之外)。
这是 jsfiddle 发生故障时的 JavaScript:
The DateAxisRenderer is really meant for line graphs, not bar graphs. When you combine it with bar graphs, it just doesn't work right. The idea / goal of the DateAxisRenderer is to make a linear / accurate time graph across a date/time. That way, if you miss a date entry, it will still be drawn to scale over time. Check their examples on the DateAxisRenderer here: http://www.jqplot.com/tests/date-axes.php
What you're wanting to use is the CategoryAxisRenderer. You can then just override / create your own tick label renderer and be good to go. Normally you wouldn't want to append extra empty items to an item, especially if they're empty, however, if you do, just append them to your data array.
Here's a jsfiddle doing what you want: http://jsfiddle.net/fordlover49/JWhmQ/
Note that you may want to look at the manage resources section to verify what files you need to reference (in addition to the jquery file).
Here's the javascript in case jsfiddle acts up:
这是我用来显示边距的一个简单技巧。
我创建了一个开始日期和一个结束日期,分别是我想要显示的内容的前一天和后一天。
例如,如果我想显示 2012 年 1 月
,那么我声明我自己的
DateTickFormatter
,我不会打印这两个日期。然后在 xaxis 中我添加以下选项:
Here is a simple hack that I used to show a margin.
I create a starting date and an ending date which are respectively one day before and one day after the contents I want to show.
For example if I want to show the month of January 2012
Then I declare my own
DateTickFormatter
where I will not printout these two dates.Then in the
xaxis
I put the following options: