【问题标题】:jqPlot : how to color parts of background / grid with several different colorsjqPlot:如何用几种不同的颜色为背景/网格的部分着色
【发布时间】:2014-08-15 20:55:24
【问题描述】:

我正在使用出色的jqPlot 插件,而我在documentation 中没有找到的唯一功能是“如何用几种不同的颜色为背景/网格的部分着色”

我的图表的 x 轴单位是日期。如果可能的话,我想通过更改背景网格颜色来突出显示网格的周末部分(仅)。

或者有人会有其他想法吗?

谢谢

编辑最终答案

根据 Boro 的建议,我开发了一种更好的方法来突出显示背景的一部分,使用“line”选项而不是“verticalLine”选项。事实是,对于“verticalLine”,开发者必须处理线宽。所以,很难得到你需要的,因为线的宽度是从线的中心而不是从边框开始的。

"Line" option 允许设置起点和终点以设置要突出显示的背景的确切部分。 width 参数仍然用于将行显示为列,但它是水平扩展的,而不是垂直扩展。

关于“lineWidth”属性,该值以像素为单位定义。因此,您可以使用图形容器高度来确定值,并将“开始”和“结束”属性的“y”值设置为系列数据的平均值或 y 轴的近似中间范围。

我更新了 Boro 的小提琴here

                grid:                 
                {
                    drawGridLines: true,        // wether to draw lines across the grid or not.
                    gridLineColor: '#cccccc',   // Color of the grid lines.
                    backgroundColor: "#eee",
                    borderColor: '#999999',     // CSS color spec for border around grid.
                    borderWidth: 2.0,           // pixel width of border around grid.
                    shadow: true,               // draw a shadow for grid.
                    shadowAngle: 45,            // angle of the shadow.  Clockwise from x axis.
                    shadowOffset: 1.5,          // offset from the line of the shadow.
                    shadowWidth: 3,             // width of the stroke for the shadow.
                    shadowDepth: 3,             // Number of strokes to make when drawing shadow.
                                                // Each stroke offset by shadowOffset from the last.
                    shadowAlpha: 0.07,          // Opacity of the shadow
                },
                canvasOverlay: {
                    show: true,
                    objects:

                            [
                                {
                                    line:
                                    {
                                        start : [new Date('2012-01-12').getTime(),20],                                                
                                        stop : [new Date('2012-01-13').getTime(),20],                                                
                                        lineWidth: 1000,
                                        color: 'rgba(255, 0, 0,0.45)',
                                        shadow: false,
                                        lineCap : 'butt'
                                    }
                                },                                    
                                {
                                    line:
                                    {
                                        start : [new Date('2012-01-13').getTime(),20],                                                
                                        stop : [new Date('2012-01-14').getTime(),20],                                                
                                        lineWidth: 1000,
                                        color: 'rgba(255, 0, 0,0.45)',
                                        shadow: false,
                                        lineCap : 'butt'
                                    }
                                }
                            ]
                }                    

【问题讨论】:

    标签: jquery jquery-plugins graph jqplot


    【解决方案1】:

    我认为@Katya 在my answer to a related problem 下的代码中显示的示例可能对您有所帮助。

    Direct link to the sample.

    编辑

    关于第二部分,您需要使用verticalLine 并将其x 值设置为毫秒,as presented here。然后你需要担心选择合适的lineWidth

    【讨论】:

    • 非常感谢 Boro,这正是我所需要的。抱歉这么晚才回复,我正在度假。
    • 嗨,仅供参考,我刚刚用我的最终解决方案编辑了我的帖子。谢谢
    • @Bouillou 非常感谢您与我们分享。非常好的代码。我看到您实际上注意到,当我尝试使用 verticalLine 时停止对我不起作用,但使用 line 它非常有用。我想知道应该如何评估lineWidth 的价值?顺便说一句,如果可以的话,我会再次 +1 问题 - 对于代码和示例。 :)
    • 线宽以像素为单位。因此,您可以设置图形容器高度属性,并将“y”值设置为系列的平均值或近似中间值。 $('#graph-container').height();
    • 这是我最初的想法,但在您的样本上尝试它时,线条仅略高于中间。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-12
    • 1970-01-01
    • 2012-07-22
    • 2015-11-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多