【问题标题】:Adding buttons to a chart - svg向图表添加按钮 - svg
【发布时间】:2016-03-27 21:28:33
【问题描述】:

我想使用 JS 库开发交互式图表。在这个图表中,我希望有按钮(在图表内),以这种方式(红色):

我还想更改线型(例如点线)。

我尝试了 Morris.js,但这个库不允许我做我想做的事。

我可以使用其他任何库吗?

【问题讨论】:

    标签: javascript svg charts morris.js


    【解决方案1】:

    Morris.js 可以做到这一点,但是您需要在 Morris(最新版本 0.5.1)中添加一些代码来绘制虚线:

    扩展 Morris 并添加一个名为 lineStyle 的参数,并在 Morris Area 配置中将此参数设置为 .

    请参阅this answer 了解 raphael.js 线条样式的可能值:

    ["", "-", ".", "-.", "-..", ". ", "- ", "--", "- .", "--.", "--.."]
    

    开启mouseover解决方案:

    (function () {
        var $, MyMorris;
    
        MyMorris = window.MyMorris = {};
        $ = jQuery;
    
        MyMorris = Object.create(Morris);
    
        MyMorris.Grid.prototype.gridDefaults["lineStyle"] = "";
     
        MyMorris.Line.prototype.drawLinePath = function(path, lineColor, lineIndex) {
          return this.raphael.path(path).attr('stroke', lineColor).attr('stroke-width', this.lineWidthForSeries(lineIndex)).attr('stroke-dasharray', this.options.lineStyle);
        };
    }).call(this);
    
    Morris.Area({
      element: 'chart',
      data: [
        { y: 'LUN', a: 1, b: 2 },
        { y: 'MAR', a: 2,  b: 3 },
        { y: 'MER', a: 4,  b: 2 },
        { y: 'JEU', a: 2,  b: 1 },
        { y: 'VEN', a: 2,  b: 2 },
        { y: 'SAM', a: 4,  b: 3 },
        { y: 'DIM', a: 1, b: 2 }
      ],
      xkey: 'y',
      ykeys: ['a', 'b'],
      labels: ['Label 1', 'Label 2'],
      fillOpacity: 0.6,
      hideHover: 'auto',
      behaveLikeLine: true,
      resize: true,
      pointFillColors: ['#ffffff'],
      pointStrokeColors: ['black'],
      lineColors: ['gray', 'blue'],
      lineStyle: ".",
      parseTime: false,
      smooth: false,
      hoverCallback: function (index, options, content, row) {
        var currentDiv = "";
        var finalContent = $("<div/>");
    
        $(content).each(function () {
    		currentDiv = $(this);
            $(finalContent).append(currentDiv);
        });
                
        var btnEdit = $("<img/>").attr("src", "http://i.stack.imgur.com/Z2AxP.png").addClass("morrisEdit").css({"cursor":"pointer"}).attr("onclick", "editAction();");
        $(finalContent).append(btnEdit);
        return finalContent;    
      }
    });
    
    function editAction() {
      alert("Edit Clicked");
      // Do some actions
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="//cdnjs.cloudflare.com/ajax/libs/raphael/2.1.0/raphael-min.js"></script>
    <script src="//cdnjs.cloudflare.com/ajax/libs/morris.js/0.5.1/morris.min.js"></script>
    <link href="//cdnjs.cloudflare.com/ajax/libs/morris.js/0.5.1/morris.css" rel="stylesheet"/>
    
    <div id="chart"></div>

    静态解:

    (function () {
        var $, MyMorris;
    
        MyMorris = window.MyMorris = {};
        $ = jQuery;
    
        MyMorris = Object.create(Morris);
    
        MyMorris.Grid.prototype.gridDefaults["lineStyle"] = "";
    
        MyMorris.Line.prototype.drawLinePath = function (path, lineColor, lineIndex) {
            return this.raphael.path(path).attr('stroke', lineColor).attr('stroke-width', this.lineWidthForSeries(lineIndex)).attr('stroke-dasharray', this.options.lineStyle);
        };
    }).call(this);
    
    var data = [
     { y: 'LUN', a: 1, b: 2 },
     { y: 'MAR', a: 2, b: 3 },
     { y: 'MER', a: 0, b: 2 },
     { y: 'JEU', a: 2, b: 1 },
     { y: 'VEN', a: 2, b: 2 },
     { y: 'SAM', a: 0, b: 0 },
     { y: 'DIM', a: 1, b: 2 }
    ];
    
    Morris.Area({
        element: 'chart',
        data: data,
        xkey: 'y',
        ykeys: ['a', 'b'],
        labels: ['Label 1', 'Label 2'],
        fillOpacity: 0.6,
        hideHover: 'auto',
        behaveLikeLine: true,
        resize: true,
        pointFillColors: ['#ffffff'],
        pointStrokeColors: ['black'],
        lineColors: ['gray', 'blue'],
        lineStyle: ".",
        parseTime: false,
        smooth: false,
    });
    
    var indexNulls = [];
    
    for (var i = 0; i < data.length; i++) {
        if (data[i].a == 0 || data[i].b == 0) {
            indexNulls.push(i);
        }
    }
    
    for (var i = 0; i < indexNulls.length; i++) {
        var circleElement = $("#chart").find("circle")[indexNulls[i]];
        var divPosition = $(circleElement).attr("cx") - 20;
        $divEdit = $("<div/>").css({ "display": "inline-block", "position": "absolute", "left": divPosition + "px" });
        $btnEdit = $("<img/>").attr("src", "http://i.stack.imgur.com/Z2AxP.png").addClass("morrisEdit").css({ "cursor": "pointer" }).attr("onclick", "editAction();");
        $divEdit.append($btnEdit);
        $("#edits").append($divEdit);
    }
    
    function editAction() {
        alert("Edit Clicked");
        // Do some actions
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="//cdnjs.cloudflare.com/ajax/libs/raphael/2.1.0/raphael-min.js"></script>
    <script src="//cdnjs.cloudflare.com/ajax/libs/morris.js/0.5.1/morris.min.js"></script>
    <link href="//cdnjs.cloudflare.com/ajax/libs/morris.js/0.5.1/morris.css" rel="stylesheet"/>
    
    <div id="chart"></div>
    <div id="edits" style="width: 100%; margin-top: -150px; position: relative;">

    【讨论】:

    • 非常感谢,您的回答对您有帮助……但是,按钮是否可能仍与 Morris.js 一起显示?
    • 不客气。我肯定已经理解了你的问题。您的意思是始终显示编辑按钮,而不仅仅是鼠标悬停?
    • 是的 .. 正是... 不只是鼠标悬停:)
    • Morris 没有内置选项可以做到这一点,所以你必须找到一个技巧。您希望按钮的具体方式、位置和操作方式是什么?
    • 我添加了一个静态解决方案:对于值为 0 的数据,按钮始终可见。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-16
    • 2012-08-06
    • 2018-01-23
    • 1970-01-01
    相关资源
    最近更新 更多