本演示页面展示了D3折线图Web Component的主要功能和配置选项。您可以通过以下步骤来体验组件的各种特性:
您可以随时点击"重置配置"按钮恢复默认设置。
<!-- 引入D3.js -->
<script src="https://cdn.jsdelivr.net/npm/d3@7"></script>
<!-- 引入d3-line-chart -->
<script src="path/to/d3-line-chart.min.js"></script>
<!-- 在HTML中使用组件 -->
<d3-line-chart id="chart"></d3-line-chart>
<script>
// 获取图表实例
const chart = document.getElementById('chart');
// 设置数据
chart.setData([
{ x: 0, y: 10 },
{ x: 1, y: 15 },
{ x: 2, y: 8 },
{ x: 3, y: 20 },
{ x: 4, y: 12 }
]);
// 设置配置
chart.setConfig({
lineColor: '#3498db',
showShadow: true,
gridColor: '#e0e0e0',
enableAnimation: true,
animationDuration: 1000,
axisTextColor: '#333333',
axisTextSize: '12px',
margin: {
top: 20,
right: 30,
bottom: 40,
left: 50
}
});
// 设置关键点
chart.setKeyPoints([
{ x: 1, y: 15, render: '<div style="background-color: red; width: 10px; height: 10px; border-radius: 50%;"></div>' },
{ x: 3, y: 20, render: '<div style="background-color: green; width: 10px; height: 10px; border-radius: 50%;"></div>' }
]);
// 设置关键刻度点
chart.setKeyTicks([
{ x: 0, label: '开始' },
{ x: 2, label: '中间' },
{ x: 4, label: '结束' }
]);
</script>