向图表添加按钮 - svg
Adding buttons to a chart - svg
我想使用 JS 库开发交互式图表。在此图表中,我想要按钮(在图表内),以这种方式(红色):
我还想更改线条类型(例如点线)。
我试过 Morris.js 但是这个库不允许我做我想做的事。
有没有我可以使用的其他库?
用Morris.js
可以,但是需要在Morris(最新版本0.5.1)中添加一些代码来绘制虚线:
扩展 Morris 并添加一个名为 lineStyle
的参数,并在 Morris Area 配置中将此参数设置为 .
。
有关 raphael.js 线型的可能值,请参见 this answer:
["", "-", ".", "-.", "-..", ". ", "- ", "--", "- .", "--.", "--.."]
关于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;">
我想使用 JS 库开发交互式图表。在此图表中,我想要按钮(在图表内),以这种方式(红色):
我还想更改线条类型(例如点线)。
我试过 Morris.js 但是这个库不允许我做我想做的事。
有没有我可以使用的其他库?
用Morris.js
可以,但是需要在Morris(最新版本0.5.1)中添加一些代码来绘制虚线:
扩展 Morris 并添加一个名为 lineStyle
的参数,并在 Morris Area 配置中将此参数设置为 .
。
有关 raphael.js 线型的可能值,请参见 this answer:
["", "-", ".", "-.", "-..", ". ", "- ", "--", "- .", "--.", "--.."]
关于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;">