Javascript 如何在使用dimple.js制作的图形的标签中添加空间?
我正在用dimple.js做一个条形图。 我的代码如下:Javascript 如何在使用dimple.js制作的图形的标签中添加空间?,javascript,dimple.js,Javascript,Dimple.js,我正在用dimple.js做一个条形图。 我的代码如下: var data = [{x:1, y:10}, {x:2, y:15}, {x:3,y:27}] var svg; var chart; var svg = dimple.newSvg("body", 800, 600); var chart = new dimple.chart(svg, data); chart.defaultColors = [ new dimple.color("#FF0000", "Blue
var data = [{x:1, y:10}, {x:2, y:15}, {x:3,y:27}]
var svg;
var chart;
var svg = dimple.newSvg("body", 800, 600);
var chart = new dimple.chart(svg, data);
chart.defaultColors = [
new dimple.color("#FF0000", "Blue"), // Set a red fill with a blue stroke
new dimple.color("#00FF00"), // Set a green fill with a darker green stroke
new dimple.color("rgb(0, 0, 255)") // Set a blue fill with a darker blue stroke
];
x = chart.addCategoryAxis("x", "x");
chart.addMeasureAxis("y", "y");
chart.addSeries("country", dimple.plot.bar);
x.addOrderRule("x");
chart.draw();
当我只有三个(或很少)数据点时,它工作得很好,但是当我有超过两百个数据点时,x轴就会被单位弄得乱七八糟。是否有办法在x标签上显示每个
n
点的单位?(所以不是显示1,2,3…而是显示1,n+1,2*n+1,…)您可以在使用一些d3绘制后修改它。以下是一种方法,该方法将删除每n个留下的标签:
// Pass in an axis object and an interval.
var cleanAxis = function (axis, oneInEvery) {
// This should have been called after draw, otherwise do nothing
if (axis.shapes.length > 0) {
// Leave the first label
var del = false;
// If there is an interval set
if (oneInEvery > 1) {
// Operate on all the axis text
axis.shapes.selectAll("text").each(function (d) {
// Remove all but the nth label
if (del % oneInEvery !== 0) {
this.remove();
// Find the corresponding tick line and remove
axis.shapes.selectAll("line").each(function (d2) {
if (d === d2) {
this.remove();
}
});
}
del += 1;
});
}
}
};
这是为你这样的案子工作的提琴:
(我还更新了您的订单规则,以避免字符串排序出现问题)您可以在使用一些d3绘图后对其进行修改。以下是一种方法,该方法将删除每n个留下的标签:
// Pass in an axis object and an interval.
var cleanAxis = function (axis, oneInEvery) {
// This should have been called after draw, otherwise do nothing
if (axis.shapes.length > 0) {
// Leave the first label
var del = false;
// If there is an interval set
if (oneInEvery > 1) {
// Operate on all the axis text
axis.shapes.selectAll("text").each(function (d) {
// Remove all but the nth label
if (del % oneInEvery !== 0) {
this.remove();
// Find the corresponding tick line and remove
axis.shapes.selectAll("line").each(function (d2) {
if (d === d2) {
this.remove();
}
});
}
del += 1;
});
}
}
};
这是为你这样的案子工作的提琴:
(我还更新了您的订单规则,以避免字符串排序出现问题)您可以在使用一些d3绘图后对其进行修改。以下是一种方法,该方法将删除每n个留下的标签:
// Pass in an axis object and an interval.
var cleanAxis = function (axis, oneInEvery) {
// This should have been called after draw, otherwise do nothing
if (axis.shapes.length > 0) {
// Leave the first label
var del = false;
// If there is an interval set
if (oneInEvery > 1) {
// Operate on all the axis text
axis.shapes.selectAll("text").each(function (d) {
// Remove all but the nth label
if (del % oneInEvery !== 0) {
this.remove();
// Find the corresponding tick line and remove
axis.shapes.selectAll("line").each(function (d2) {
if (d === d2) {
this.remove();
}
});
}
del += 1;
});
}
}
};
这是为你这样的案子工作的提琴:
(我还更新了您的订单规则,以避免字符串排序出现问题)您可以在使用一些d3绘图后对其进行修改。以下是一种方法,该方法将删除每n个留下的标签:
// Pass in an axis object and an interval.
var cleanAxis = function (axis, oneInEvery) {
// This should have been called after draw, otherwise do nothing
if (axis.shapes.length > 0) {
// Leave the first label
var del = false;
// If there is an interval set
if (oneInEvery > 1) {
// Operate on all the axis text
axis.shapes.selectAll("text").each(function (d) {
// Remove all but the nth label
if (del % oneInEvery !== 0) {
this.remove();
// Find the corresponding tick line and remove
axis.shapes.selectAll("line").each(function (d2) {
if (d === d2) {
this.remove();
}
});
}
del += 1;
});
}
}
};
这是为你这样的案子工作的提琴:
(我还更新了您的订购规则,以避免字符串订购出现问题)太棒了!这正是我需要的。谢谢!非常感谢,约翰。能够很好地将其应用于y轴:真讨厌!这正是我需要的。谢谢!非常感谢,约翰。能够很好地将其应用于y轴:真讨厌!这正是我需要的。谢谢!非常感谢,约翰。能够很好地将其应用于y轴:真讨厌!这正是我需要的。谢谢!非常感谢,约翰。能够很好地将其应用于y轴:D