javascript - 将折线图/面积图线延伸到图表边缘

标签 javascript svg charts highcharts

我正在根据此引用图像制作自定义样式的折线图/面积图:

Chart reference

我已经实现了大部分,但是,我正在努力处理延伸到图表边缘的线条。

这是我的 fiddle :http://jsbin.com/ePilePe/3/edit?js,output

正如您在图像中看到的那样,X 轴上的第一个点和最后一个点有一条射线延伸到图表的末尾,而在我的 fiddle 中没有这样的东西。

我已经研究 API 和文档很长时间了,但找不到任何相关信息。

我将如何实现这种行为?

最佳答案

经过一些摆弄之后,我绕过了 Highcharts 以获得预期的效果。

这是我的 fiddle :http://jsbin.com/upUFIbO/1/ .

我所做的是,添加以下内容作为 loadredraw 事件的回调:

/**
 * Helper that extends area and lines to the left and right plot edges.
 *
 * Has to be invoked with .call(), giving Highchart as the this reference
 * and Highchart event as the second parameter.
 * 
 * @return void
 */
var edgeExtend = function(e)
{
  for (var l = this.series.length, i = 0; i< l; i++)
  {
    // Get current series.
    var series = this.series[i];

    // Get inner-chart box.
    var box = this.plotBox;

    // Take areas path.
    var areaPath = series.areaPath;

    // Add start point.
    // Right after the first element (M).
    areaPath.splice(1, 0, 0, areaPath[2], 'L');

    // Add Last points upper area end.
    // Remove penultimate point.
    // Replace it with a new point reaching to the width of chart and growing to the height of last element.
    // And add the bottom-right corner.
    areaPath.splice(-6, 3, 'L', box.width, areaPath[areaPath.length - 7], 'L', box.width, box.height);

    // Make the last points X be zero - that will result in bottom left corner.
    areaPath[areaPath.length - 2] = 0;

    // Replace value (redraw).
    series.area.element.attributes.d.value = areaPath.join(' ');

    var graphPath = series.graphPath;

    // Add start point.
    // Right after the first element (M).
    graphPath.splice(1, 0, 0, graphPath[2], 'L');

    // Add end point.
    graphPath.push('L', box.width, graphPath[graphPath.length - 1]);

    series.graph.element.attributes.d.value = graphPath.join(' ');
  }
};

可能这不是最好的方法,但是,嘿,直接矢量路径操作不会出错。

如果有人对问题有更“用户友好”的解决方案,我很乐意看到。

关于javascript - 将折线图/面积图线延伸到图表边缘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18463741/

相关文章:

charts - Google 图表/可视化 api Interpolate Null 仅适用于某些数据点?

python - 如何创建阿拉伯图表? (x 轴从右到左)与 Plotly

javascript - 动画jquery只工作一次

javascript - 哪个是最适合测量报告 Web 应用程序的 javascript 小部件库?

html - React - svg 内的 html 标签

javascript - 如何使用java脚本将svg数据转换为图像/图像base64

css - 如何剪辑图像并使其响应?

javascript - 存储用户之前是否按下过按钮

javascript - 引导模式中的引导日期选择器

javascript - 从数组值中获取值以形成聊天