Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
274 views
in Technique[技术] by (71.8m points)

javascript - Adding buttons to a chart - svg

I want to develop an interactive chart using a JS library. In this chart I want to have buttons (inside the chart), in this way (in red):

enter image description here

I want also to change the line type (dotted for example).

I tried Morris.js but this lib is not allowing me to do what I want to do.

Is there any other library that I can use instead ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

That's possible with Morris.js, but you need to add some code to Morris (latest version 0.5.1) to draw dotted lines:

Extend Morris and add a parameter called lineStyle and set this parameter to . in the Morris Area configuration.

See this answer for the possible values for raphael.js line style:

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

On mouseover solution:

(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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...