我正在使用 d3 库编写一个图表,您可以在 d3js.org 上找到该库 我希望创建一个名为的 vue 组件,它将显示一个圆环图 制作它的代码相当大,所以我想将它放在一个单独的js文件中并将其导入为 我的甜甜圈图组件中的一个函数。我的问题是,当我这样做时,我有一个错误告诉 我正在尝试迭代一个空数据集。 稍后我计划做一些sql查询。 下面你可以看到我的代码:
我的js文件
<!--index.js-->
import * as d3 from "d3";
function PieChart(data, {
name = ([x]) => x, // given d in data, returns the (ordinal) label
value = ([, y]) => y, // given d in data, returns the (quantitative) value
title, // given d in data, returns the title text
width = 500, // outer width, in pixels
height = 500, // outer height, in pixels
innerRadius = 0.25*Math.min(height,width), // inner radius of pie, in pixels (non-zero for donut)
outerRadius = Math.min(width, height) / 2, // outer radius of pie, in pixels
labelRadius = (innerRadius * 0.2 + outerRadius * 0.8), // center radius of labels
format = ",", // a format specifier for values (in the label)
names, // array of names (the domain of the color scale)
colors, // array of colors for names
stroke = innerRadius > 0 ? "none" : "white", // stroke separating widths
strokeWidth = 1, // width of stroke separating wedges
strokeLinejoin = "round", // line join of stroke separating wedges
padAngle = stroke === "none" ? 1 / outerRadius : 0, // angular separation between wedges
} = {}) {
// Compute values.
const N = d3.map(data, name);
const V = d3.map(data, value);
const I = d3.range(N.length).filter(i => !isNaN(V[i]));
// Unique the names.
if (names === undefined) names = N;
names = new d3.InternSet(names);
// Chose a default color scheme based on cardinality.
if (colors === undefined) colors = d3.schemeSpectral[names.size];
if (colors === undefined) colors = d3.quantize(t => d3.interpolateSpectral(t * 0.8 + 0.1), names.size);
// Construct scales.
const color = d3.scaleOrdinal(names, colors);
// Compute titles.
if (title === undefined) {
const formatValue = d3.format(format);
title = i => `${N[i]}\n${formatValue(V[i])}`;
} else {
const O = d3.map(data, d => d);
const T = title;
title = i => T(O[i], i, data);
}
// Construct arcs.
const arcs = d3.pie().padAngle(padAngle).sort(null).value(i => V[i])(I);
const arc = d3.arc().innerRadius(innerRadius).outerRadius(outerRadius);
const arcLabel = d3.arc().innerRadius(labelRadius).outerRadius(labelRadius);
const svg = d3.create("svg")
.attr("width", width)
.attr("height", height)
.attr("viewBox", [-width / 2, -height / 2, width, height])
.attr("style", "max-width: 100%; height: auto; height: intrinsic;");
svg.append("g")
.attr("stroke", stroke)
.attr("stroke-width", strokeWidth)
.attr("stroke-linejoin", strokeLinejoin)
.selectAll("path")
.data(arcs)
.join("path")
.attr("fill", d => color(N[d.data]))
.attr("d", arc)
.append("title")
.text(d => title(d.data));
svg.append("g")
.attr("font-family", "sans-serif")
.attr("font-size", 10)
.attr("text-anchor", "middle")
.selectAll("text")
.data(arcs)
.join("text")
.attr("transform", d => `translate(${arcLabel.centroid(d)})`)
.selectAll("tspan")
.data(d => {
const lines = `${title(d.data)}`.split(/\n/);
return (d.endAngle - d.startAngle) > 0.25 ? lines : lines.slice(0, 1);
})
.join("tspan")
.attr("x", 0)
.attr("y", (_, i) => `${i * 1.1}em`)
.attr("font-weight", (_, i) => i ? null : "bold")
.text(d => d);
return Object.assign(svg.node(), {scales: {color}});
}
export default PieChart;
我的甜甜圈图表组件
<template>
<div id="chart">
{{ PieChart() }}
</div>
</template>
<script>
const data = [{
name : "toto",
value: 10
},
{
name : "tato",
value: 10
},
{
name : "tota",
value: 10
}];
PieChart( data, {
name: d => d.name,
value: d => Math.round(100*d.value/30),
width: 400,
height: 400
})
export default {
name: 'DoughnutChart',
methods : {
PieChart,
}
}
import PieChart from './index.js';
</script>
我的视图组件
<template>
<DoughnutChart></DoughnutChart>
</template>
<script>
//export
//import
import DoughnutChart from '@/components/Charts/DoughnutChart/DoughnutChart.vue';
export default {
name: 'myBudgetView',
components: {
DoughnutChart
},
}
</script>
<style scoped>
#chart{
background-color: white;
border-radius: 10px;
padding: 20px;
margin: 20px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
width: 75%;
height: 75%;
}
</style>
为了解决这个问题,我编写了一个名为
build()
的函数
没有这样的参数:
build() {
return PieChart(this.param1, this.param2)
}
在我的
data()
函数中定义了 param1 和 param2。