Line charts display data as points connected by lines, ideal for showing trends over time.
Basic Usage
To visualize data with a line chart, use type: 'line'. Line charts require
both start and value (displayed as --size).
await import('/components/data-display/chart.js');
const { tags, $ } = Lightview;
const { Chart, span } = tags;
const chart = Chart({
type: 'line',
labels: true,
primaryAxis: true,
style: 'width: 100%; max-width: 600px; height: 250px; margin: 0 auto;'
},
Chart.Body({},
Chart.Row({},
Chart.Label({}, 'Jan'),
Chart.Data({ start: 0.2, value: 0.4 }, span({ class: 'data' }, '40%'))
),
Chart.Row({},
Chart.Label({}, 'Feb'),
Chart.Data({ start: 0.4, value: 0.6 }, span({ class: 'data' }, '60%'))
),
Chart.Row({},
Chart.Label({}, 'Mar'),
Chart.Data({ start: 0.6, value: 0.5 }, span({ class: 'data' }, '50%'))
),
Chart.Row({},
Chart.Label({}, 'Apr'),
Chart.Data({ start: 0.5, value: 0.8 }, span({ class: 'data' }, '80%'))
),
Chart.Row({},
Chart.Label({}, 'May'),
Chart.Data({ start: 0.8, value: 0.7 }, span({ class: 'data' }, '70%'))
)
)
);
$('#example').content(chart);
Multiple Datasets
Add multiple lines by using multiple Chart.Data elements with
multiple: true.
await import('/components/data-display/chart.js');
const { tags, $ } = Lightview;
const { Chart } = tags;
const chart = Chart({
type: 'line',
multiple: true,
labels: true,
primaryAxis: true,
style: 'width: 100%; max-width: 600px; height: 280px; margin: 0 auto; --color-1: #4CAF50; --color-2: #2196F3; --color-3: #FF9800;'
},
Chart.Body({},
Chart.Row({},
Chart.Label({}, 'Q1'),
Chart.Data({ start: 0.2, value: 0.4 }),
Chart.Data({ start: 0.3, value: 0.5 }),
Chart.Data({ start: 0.1, value: 0.3 })
),
Chart.Row({},
Chart.Label({}, 'Q2'),
Chart.Data({ start: 0.4, value: 0.5 }),
Chart.Data({ start: 0.5, value: 0.6 }),
Chart.Data({ start: 0.3, value: 0.5 })
),
Chart.Row({},
Chart.Label({}, 'Q3'),
Chart.Data({ start: 0.5, value: 0.7 }),
Chart.Data({ start: 0.6, value: 0.4 }),
Chart.Data({ start: 0.5, value: 0.6 })
),
Chart.Row({},
Chart.Label({}, 'Q4'),
Chart.Data({ start: 0.7, value: 0.9 }),
Chart.Data({ start: 0.4, value: 0.7 }),
Chart.Data({ start: 0.6, value: 0.8 })
)
)
);
$('#example').content(chart);
Axes and Grid
Add grid lines with secondaryAxis.
await import('/components/data-display/chart.js');
const { tags, $ } = Lightview;
const { Chart } = tags;
const chart = Chart({
type: 'line',
labels: true,
primaryAxis: true,
secondaryAxesCount: 10,
style: 'width: 100%; max-width: 600px; height: 280px; margin: 0 auto;'
},
Chart.Body({},
Chart.Row({}, Chart.Label({}, '1'), Chart.Data({ start: 0.1, value: 0.3 })),
Chart.Row({}, Chart.Label({}, '2'), Chart.Data({ start: 0.3, value: 0.5 })),
Chart.Row({}, Chart.Label({}, '3'), Chart.Data({ start: 0.5, value: 0.4 })),
Chart.Row({}, Chart.Label({}, '4'), Chart.Data({ start: 0.4, value: 0.7 })),
Chart.Row({}, Chart.Label({}, '5'), Chart.Data({ start: 0.7, value: 0.6 })),
Chart.Row({}, Chart.Label({}, '6'), Chart.Data({ start: 0.6, value: 0.9 }))
)
);
$('#example').content(chart);
Line Styling
Customize line appearance with CSS custom properties.
await import('/components/data-display/chart.js');
const { tags, $ } = Lightview;
const { Chart } = tags;
const chart = Chart({
type: 'line',
labels: true,
style: 'width: 100%; max-width: 600px; height: 250px; margin: 0 auto; --color: #7480ff; --line-size: 3px;'
},
Chart.Body({},
Chart.Row({}, Chart.Label({}, 'Jan'), Chart.Data({ start: 0.3, value: 0.5 })),
Chart.Row({}, Chart.Label({}, 'Feb'), Chart.Data({ start: 0.5, value: 0.7 })),
Chart.Row({}, Chart.Label({}, 'Mar'), Chart.Data({ start: 0.7, value: 0.6 })),
Chart.Row({}, Chart.Label({}, 'Apr'), Chart.Data({ start: 0.6, value: 0.9 })),
Chart.Row({}, Chart.Label({}, 'May'), Chart.Data({ start: 0.9, value: 0.8 }))
)
);
$('#example').content(chart);