<vega-line-chart id="line-chart" style="width:100%;height:300px"></vega-line-chart>
document.querySelector('#line-chart').options = {
dataSource: [{ x:10, y:50 },{ x: 20, y:25 }, { x: 30, y:40 }],
legends: ['Legend One'],
xAxis:{
type:'value',
labelFormatter: text=>text+'%'
},
yAxis:{
labelFormatter: text=>text+'%'
}
};
<vega-line-chart id="stacked-line-chart" style="width:500px;height:300px"></vega-line-chart>
const mockData = [
{
x: 10,
y: 0.1,
},
{
x: 20,
y: 0.2,
},
{
x: 30,
y: 1,
},
{
x: 45,
y: 50,
},
{
x: 21,
y: 100,
},
{
x: 50,
y: 150,
},
{
x: 66,
y: 200,
},
{
x: 13,
y: 150,
},
{
x: 80,
y: 50,
},
{
x: 100,
y: 20,
},
{
x: 110,
y: 5,
},
{
x: 40,
y: 1,
},
{
x: 15,
y: 20,
},
];
const stackMockData = [mockData, mockData.map((item, index) => ({ ...item, y: item.y + index * 10 }))];
document.getElementById("stacked-line-chart").options = {
dataSource: stackMockData,
legends: ["Fries $123.00", "Chicken Burger $105.00"],
};
Line charts are a common visualization tool for displaying and analyizing data over an interval or time period. They are particularly helpful for representing trends or relationships between two variables.
Name | Description | Default | |
---|---|---|---|
options* | data source optionsrequired |
||
Options | |||
layout | The chart layout{width:number, height:number, margin:{top:number, left:number, right:number, bottom:number}} |
- | - |
legends | The chart legends, required if legends need to be displayedstring[] |
- | - |
dataSource* | The chart data source{x:number, y:number}[](basic line) \| {x:number, y:number}[][](stacked line) |
- | - |
colors | The chart line colorvega design backgroud color token array |
bg-accent1-primary bg-accent2-primary bg-accent3-primary bg-accent4-primary bg-accent5-primary bg-accent6-primary bg-accent7-primary bg-accent8-primary |
|
xAxis | The chart x axis setting{ type: value\|time\|category ,labelFormatter: text=> text } |
||
yAxis | The chart y axis setting{labelFormatter: text=> text} |
||
Layout | |||
width | The chart widthnumber or length string, such as 100, "100px" or "100%" |
||
height | The chart heightnumber or length string, such as 100, "100px" or "100%" |
||
margin | The chart marginThe margin properties: top, right, bottom, right must be numbers |
{top:30(No legends)/60(Has legends),right:30,bottom:30,left:60} |
const ChartOptionsType = {
legends: string[];
colors: string[];
layout: {
width: number;
height: number;
margin: {
top:number;
right:number;
bottom:number;
left:number;
}
};
dataSource: {
x:number | date string | string, //date string 2023-04-16
y:number
}[],
xAxis:{
// the type match the dataSource property x, value->number, time->date string, string->category, default type is value
type: 'value' | 'time' | 'category'
// axis lable function, such as (text:string)=>'$'+text
labelFormatter:(text:number|Date|string):string=>text
},
yAxis:{
// axis lable function, such as (text:string)=>text+'%'
labelFormatter:(text:number|Date|string):string=>text
}
}
reRender
- Updates the chart using existing options.
The default width of the chart is the same as the parent element width. As a result, the chart may be rendered blank if the parent element is hidden, such as if being rendered in a modal or tab. In this case you will need to call the reRender
method after the tab or modal is shown.
For example:
<vega-modal>
<div>
<vega-line-chart id="line-chart"></vega-line-chart>
</div>
</vega-modal>
<script>
document.querySelector('#line-chart').options = mockOptions;
//after the modal is displayed
document.querySelector('vega-modal').addEventListener('vegaOpen', () => {
document.querySelector('#line-chart').reRender();
});
</script>