柱状图
用从基线伸出的柱条比较数值,支持并排、堆叠和横向布局。
本页内容
柱状图适合比较一周中每天或一年中各季度的数值。每根柱都从基线起,居于对应的类别带中央,远离基线的一端采用圆角。指针移入时,其他柱会立即淡化。指针命中的是整个类别带,因此短柱和长柱一样容易查看。
柱状图
<div class="card max-w(440) mx-auto"> <div class="card-header"> <h3 class="card-title">Deployments</h3> <p class="card-description">311 this week</p> </div> <figure class="chart" id="deployments" style="--chart-height: 11rem"></figure></div><script>addEventListener('DOMContentLoaded', function () {MasterUI.chart(document.getElementById('deployments'), { type: 'bar', label: 'Deployments per day, this week', labels: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'], series: [{ name: 'Deployments', values: [42, 58, 36, 71, 64, 22, 18] }]})})</script><script type="module"> import { attach } from '@master/ui/behaviors' attach()</script>import { Chart } from './ui/chart'<div className="card max-w(440) mx-auto"> <div className="card-header"> <h3 className="card-title">Deployments</h3> <p className="card-description">311 this week</p> </div> <Chart id="deployments" style={{ '--chart-height': '11rem' } as React.CSSProperties} type="bar" label="Deployments per day, this week" labels={['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']} series={[{ name: 'Deployments', values: [42, 58, 36, 71, 64, 22, 18] }]} /></div><script> import Chart from './ui/Chart.svelte'</script><div class="card max-w(440) mx-auto"> <div class="card-header"> <h3 class="card-title">Deployments</h3> <p class="card-description">311 this week</p> </div> <Chart id="deployments" style="--chart-height: 11rem" type="bar" label="Deployments per day, this week" labels={['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']} series={[{ name: 'Deployments', values: [42, 58, 36, 71, 64, 22, 18] }]} /></div><script setup>import Chart from './ui/Chart.vue'</script><template> <div class="card max-w(440) mx-auto"> <div class="card-header"> <h3 class="card-title">Deployments</h3> <p class="card-description">311 this week</p> </div> <Chart id="deployments" style="--chart-height: 11rem" type="bar" label="Deployments per day, this week" :labels="['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']" :series="[{ name: 'Deployments', values: [42, 58, 36, 71, 64, 22, 18] }]" /> </div></template>有负值时,基线位于零刻度处,负值柱从基线向下延伸。零值会在基线上留一道短线,因此没有数值的那一天也有标记。
零值上下
<div class="card max-w(440) mx-auto"> <div class="card-header"> <h3 class="card-title">Net change</h3> <p class="card-description">Accounts gained and lost per month</p> </div> <figure class="chart" id="net" style="--chart-height: 11rem"></figure></div><script>addEventListener('DOMContentLoaded', function () {MasterUI.chart(document.getElementById('net'), { type: 'bar', label: 'Net change in accounts per month', labels: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug'], series: [{ name: 'Net change', values: [12, -8, 20, -15, 6, 0, 18, 9] }]})})</script><script type="module"> import { attach } from '@master/ui/behaviors' attach()</script>import { Chart } from './ui/chart'<div className="card max-w(440) mx-auto"> <div className="card-header"> <h3 className="card-title">Net change</h3> <p className="card-description">Accounts gained and lost per month</p> </div> <Chart id="net" style={{ '--chart-height': '11rem' } as React.CSSProperties} type="bar" label="Net change in accounts per month" labels={['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug']} series={[{ name: 'Net change', values: [12, -8, 20, -15, 6, 0, 18, 9] }]} /></div><script> import Chart from './ui/Chart.svelte'</script><div class="card max-w(440) mx-auto"> <div class="card-header"> <h3 class="card-title">Net change</h3> <p class="card-description">Accounts gained and lost per month</p> </div> <Chart id="net" style="--chart-height: 11rem" type="bar" label="Net change in accounts per month" labels={['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug']} series={[{ name: 'Net change', values: [12, -8, 20, -15, 6, 0, 18, 9] }]} /></div><script setup>import Chart from './ui/Chart.vue'</script><template> <div class="card max-w(440) mx-auto"> <div class="card-header"> <h3 class="card-title">Net change</h3> <p class="card-description">Accounts gained and lost per month</p> </div> <Chart id="net" style="--chart-height: 11rem" type="bar" label="Net change in accounts per month" :labels="['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug']" :series="[{ name: 'Net change', values: [12, -8, 20, -15, 6, 0, 18, 9] }]" /> </div></template>#突出一根柱
给图表添加 chart-quiet 类可淡化各柱,仅为标记了 aria-current 的数据点保留墨色,例如一周中的今天,或读者当前使用的方案。通过 current 指定该数据点的索引。values: true 会在每根柱的末端显示数值,适合柱数较少且需要看清具体数值的图表,也适合没有悬停交互的触摸屏。
弱化其他柱并显示数值
<div class="card max-w(440) mx-auto"> <div class="card-header"> <h3 class="card-title">Build minutes</h3> <p class="card-description">Thursday so far</p> </div> <figure class="chart chart-quiet" id="minutes" style="--chart-height: 10rem"></figure></div><script>addEventListener('DOMContentLoaded', function () {MasterUI.chart(document.getElementById('minutes'), { type: 'bar', current: 3, values: true, label: 'Build minutes per day, this week', labels: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'], series: [{ name: 'Minutes', values: [62, 48, 75, 54, 88, 30, 26] }]})})</script><script type="module"> import { attach } from '@master/ui/behaviors' attach()</script>import { Chart } from './ui/chart'<div className="card max-w(440) mx-auto"> <div className="card-header"> <h3 className="card-title">Build minutes</h3> <p className="card-description">Thursday so far</p> </div> <Chart className="chart-quiet" id="minutes" style={{ '--chart-height': '10rem' } as React.CSSProperties} type="bar" current={3} values label="Build minutes per day, this week" labels={['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']} series={[{ name: 'Minutes', values: [62, 48, 75, 54, 88, 30, 26] }]} /></div><script> import Chart from './ui/Chart.svelte'</script><div class="card max-w(440) mx-auto"> <div class="card-header"> <h3 class="card-title">Build minutes</h3> <p class="card-description">Thursday so far</p> </div> <Chart class="chart-quiet" id="minutes" style="--chart-height: 10rem" type="bar" current={3} values label="Build minutes per day, this week" labels={['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']} series={[{ name: 'Minutes', values: [62, 48, 75, 54, 88, 30, 26] }]} /></div><script setup>import Chart from './ui/Chart.vue'</script><template> <div class="card max-w(440) mx-auto"> <div class="card-header"> <h3 class="card-title">Build minutes</h3> <p class="card-description">Thursday so far</p> </div> <Chart class="chart-quiet" id="minutes" style="--chart-height: 10rem" type="bar" :current="3" values label="Build minutes per day, this week" :labels="['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']" :series="[{ name: 'Minutes', values: [62, 48, 75, 54, 88, 30, 26] }]" /> </div></template>#并排或堆叠
多个数据系列在每个类别带内并排显示,方便比较各系列。启用 stacked 后,各系列依次堆叠,便于比较总量及其构成。每组堆叠只有最外侧的柱端带圆角,各柱之间留有一道露出背景的间隙。数值提示按图中从上到下的顺序列出各项。
并排
<div class="card max-w(440) mx-auto"> <div class="card-header"> <h3 class="card-title">Plan</h3> <p class="card-description">Actual against plan, by quarter</p> </div> <figure class="chart" id="plan" style="--chart-height: 11rem"></figure></div><script>addEventListener('DOMContentLoaded', function () {MasterUI.chart(document.getElementById('plan'), { type: 'bar', label: 'Planned and actual revenue by quarter, in thousands of dollars', labels: ['Q1', 'Q2', 'Q3', 'Q4'], series: [ { name: 'Plan', values: [40, 55, 60, 75] }, { name: 'Actual', values: [36, 58, 71, 69] } ]})})</script><script type="module"> import { attach } from '@master/ui/behaviors' attach()</script>import { Chart } from './ui/chart'<div className="card max-w(440) mx-auto"> <div className="card-header"> <h3 className="card-title">Plan</h3> <p className="card-description">Actual against plan, by quarter</p> </div> <Chart id="plan" style={{ '--chart-height': '11rem' } as React.CSSProperties} type="bar" label="Planned and actual revenue by quarter, in thousands of dollars" labels={['Q1', 'Q2', 'Q3', 'Q4']} series={[ { name: 'Plan', values: [40, 55, 60, 75] }, { name: 'Actual', values: [36, 58, 71, 69] } ]} /></div><script> import Chart from './ui/Chart.svelte'</script><div class="card max-w(440) mx-auto"> <div class="card-header"> <h3 class="card-title">Plan</h3> <p class="card-description">Actual against plan, by quarter</p> </div> <Chart id="plan" style="--chart-height: 11rem" type="bar" label="Planned and actual revenue by quarter, in thousands of dollars" labels={['Q1', 'Q2', 'Q3', 'Q4']} series={[ { name: 'Plan', values: [40, 55, 60, 75] }, { name: 'Actual', values: [36, 58, 71, 69] } ]} /></div><script setup>import Chart from './ui/Chart.vue'</script><template> <div class="card max-w(440) mx-auto"> <div class="card-header"> <h3 class="card-title">Plan</h3> <p class="card-description">Actual against plan, by quarter</p> </div> <Chart id="plan" style="--chart-height: 11rem" type="bar" label="Planned and actual revenue by quarter, in thousands of dollars" :labels="['Q1', 'Q2', 'Q3', 'Q4']" :series="[ { name: 'Plan', values: [40, 55, 60, 75] }, { name: 'Actual', values: [36, 58, 71, 69] } ]" /> </div></template>堆叠柱状图
<div class="card max-w(440) mx-auto"> <div class="card-header"> <h3 class="card-title">Tickets</h3> <p class="card-description">Opened per day, by priority</p> </div> <figure class="chart" id="tickets" style="--chart-height: 11rem"></figure></div><script>addEventListener('DOMContentLoaded', function () {MasterUI.chart(document.getElementById('tickets'), { type: 'bar', stacked: true, label: 'Tickets opened per day by priority', labels: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'], series: [ { name: 'Low', values: [12, 18, 14, 22, 19, 8, 6] }, { name: 'Normal', values: [20, 24, 18, 30, 26, 10, 9] }, { name: 'Urgent', values: [5, 9, 4, 12, 10, 3, 2] } ]})})</script><script type="module"> import { attach } from '@master/ui/behaviors' attach()</script>import { Chart } from './ui/chart'<div className="card max-w(440) mx-auto"> <div className="card-header"> <h3 className="card-title">Tickets</h3> <p className="card-description">Opened per day, by priority</p> </div> <Chart id="tickets" style={{ '--chart-height': '11rem' } as React.CSSProperties} type="bar" stacked label="Tickets opened per day by priority" labels={['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']} series={[ { name: 'Low', values: [12, 18, 14, 22, 19, 8, 6] }, { name: 'Normal', values: [20, 24, 18, 30, 26, 10, 9] }, { name: 'Urgent', values: [5, 9, 4, 12, 10, 3, 2] } ]} /></div><script> import Chart from './ui/Chart.svelte'</script><div class="card max-w(440) mx-auto"> <div class="card-header"> <h3 class="card-title">Tickets</h3> <p class="card-description">Opened per day, by priority</p> </div> <Chart id="tickets" style="--chart-height: 11rem" type="bar" stacked label="Tickets opened per day by priority" labels={['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']} series={[ { name: 'Low', values: [12, 18, 14, 22, 19, 8, 6] }, { name: 'Normal', values: [20, 24, 18, 30, 26, 10, 9] }, { name: 'Urgent', values: [5, 9, 4, 12, 10, 3, 2] } ]} /></div><script setup>import Chart from './ui/Chart.vue'</script><template> <div class="card max-w(440) mx-auto"> <div class="card-header"> <h3 class="card-title">Tickets</h3> <p class="card-description">Opened per day, by priority</p> </div> <Chart id="tickets" style="--chart-height: 11rem" type="bar" stacked label="Tickets opened per day by priority" :labels="['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']" :series="[ { name: 'Low', values: [12, 18, 14, 22, 19, 8, 6] }, { name: 'Normal', values: [20, 24, 18, 30, 26, 10, 9] }, { name: 'Urgent', values: [5, 9, 4, 12, 10, 3, 2] } ]" /> </div></template>#按占比堆叠
stacked: 'percent' 将每组堆叠归一化为 100%。每根柱的高度对应它在本组中的占比,因此各组总高度相同,可以直接比较构成。数值提示会同时显示原始数值和占比。
占比
<div class="card max-w(440) mx-auto"> <div class="card-header"> <h3 class="card-title">Accounts</h3> <p class="card-description">Share by plan, each quarter</p> </div> <figure class="chart" id="plans" style="--chart-height: 11rem"></figure></div><script>addEventListener('DOMContentLoaded', function () {MasterUI.chart(document.getElementById('plans'), { type: 'bar', stacked: 'percent', label: 'Share of accounts by plan, each quarter', labels: ['Q1', 'Q2', 'Q3', 'Q4'], series: [ { name: 'Free', values: [620, 640, 610, 580] }, { name: 'Pro', values: [240, 300, 360, 420] }, { name: 'Team', values: [60, 90, 130, 190] } ]})})</script><script type="module"> import { attach } from '@master/ui/behaviors' attach()</script>import { Chart } from './ui/chart'<div className="card max-w(440) mx-auto"> <div className="card-header"> <h3 className="card-title">Accounts</h3> <p className="card-description">Share by plan, each quarter</p> </div> <Chart id="plans" style={{ '--chart-height': '11rem' } as React.CSSProperties} type="bar" stacked="percent" label="Share of accounts by plan, each quarter" labels={['Q1', 'Q2', 'Q3', 'Q4']} series={[ { name: 'Free', values: [620, 640, 610, 580] }, { name: 'Pro', values: [240, 300, 360, 420] }, { name: 'Team', values: [60, 90, 130, 190] } ]} /></div><script> import Chart from './ui/Chart.svelte'</script><div class="card max-w(440) mx-auto"> <div class="card-header"> <h3 class="card-title">Accounts</h3> <p class="card-description">Share by plan, each quarter</p> </div> <Chart id="plans" style="--chart-height: 11rem" type="bar" stacked="percent" label="Share of accounts by plan, each quarter" labels={['Q1', 'Q2', 'Q3', 'Q4']} series={[ { name: 'Free', values: [620, 640, 610, 580] }, { name: 'Pro', values: [240, 300, 360, 420] }, { name: 'Team', values: [60, 90, 130, 190] } ]} /></div><script setup>import Chart from './ui/Chart.vue'</script><template> <div class="card max-w(440) mx-auto"> <div class="card-header"> <h3 class="card-title">Accounts</h3> <p class="card-description">Share by plan, each quarter</p> </div> <Chart id="plans" style="--chart-height: 11rem" type="bar" stacked="percent" label="Share of accounts by plan, each quarter" :labels="['Q1', 'Q2', 'Q3', 'Q4']" :series="[ { name: 'Free', values: [620, 640, 610, 580] }, { name: 'Pro', values: [240, 300, 360, 420] }, { name: 'Team', values: [60, 90, 130, 190] } ]" /> </div></template>#向零值两侧延伸
负值从基线向下堆叠,正值向上堆叠。这样,符号相反的两个数据系列就会从中间的零线向两侧展开,例如赞成与反对、收入与支出。为各系列显式指定类别,使两侧的数据无论出现在什么位置都保持各自的颜色。
向零值两侧延伸
<div class="card max-w(520) mx-auto"> <div class="card-header"> <h3 class="card-title">Survey</h3> <p class="card-description">Share who disagree and who agree</p> </div> <figure class="chart" id="survey" style="--chart-height: 11rem"></figure></div><script>addEventListener('DOMContentLoaded', function () {MasterUI.chart(document.getElementById('survey'), { type: 'bar', stacked: true, horizontal: true, label: 'Share of answers that disagree and agree, by statement, in percent', labels: ['Easy to set up', 'Fast', 'Well documented', 'Good value'], series: [ { name: 'Disagree', category: 'orange', values: [-12, -24, -18, -30] }, { name: 'Agree', category: 'blue', values: [72, 58, 64, 46] } ]})})</script><script type="module"> import { attach } from '@master/ui/behaviors' attach()</script>import { Chart } from './ui/chart'<div className="card max-w(520) mx-auto"> <div className="card-header"> <h3 className="card-title">Survey</h3> <p className="card-description">Share who disagree and who agree</p> </div> <Chart id="survey" style={{ '--chart-height': '11rem' } as React.CSSProperties} type="bar" stacked horizontal label="Share of answers that disagree and agree, by statement, in percent" labels={['Easy to set up', 'Fast', 'Well documented', 'Good value']} series={[ { name: 'Disagree', category: 'orange', values: [-12, -24, -18, -30] }, { name: 'Agree', category: 'blue', values: [72, 58, 64, 46] } ]} /></div><script> import Chart from './ui/Chart.svelte'</script><div class="card max-w(520) mx-auto"> <div class="card-header"> <h3 class="card-title">Survey</h3> <p class="card-description">Share who disagree and who agree</p> </div> <Chart id="survey" style="--chart-height: 11rem" type="bar" stacked horizontal label="Share of answers that disagree and agree, by statement, in percent" labels={['Easy to set up', 'Fast', 'Well documented', 'Good value']} series={[ { name: 'Disagree', category: 'orange', values: [-12, -24, -18, -30] }, { name: 'Agree', category: 'blue', values: [72, 58, 64, 46] } ]} /></div><script setup>import Chart from './ui/Chart.vue'</script><template> <div class="card max-w(520) mx-auto"> <div class="card-header"> <h3 class="card-title">Survey</h3> <p class="card-description">Share who disagree and who agree</p> </div> <Chart id="survey" style="--chart-height: 11rem" type="bar" stacked horizontal label="Share of answers that disagree and agree, by statement, in percent" :labels="['Easy to set up', 'Fast', 'Well documented', 'Good value']" :series="[ { name: 'Disagree', category: 'orange', values: [-12, -24, -18, -30] }, { name: 'Agree', category: 'blue', values: [72, 58, 64, 46] } ]" /> </div></template>#横向条形图
horizontal 将图表改为横向条形图,每个名称占一行,适合名称长于柱形类别带宽度的排名。名称显示在各行旁边,刻度在下方。超过 18 个字符的名称会被截断,数值提示中会显示全名。
横向条形图
<div class="card max-w(520) mx-auto"> <div class="card-header"> <h3 class="card-title">Top pages</h3> <p class="card-description">Visitors in the last 7 days</p> </div> <figure class="chart" id="pages" style="--chart-height: 11rem"></figure></div><script>addEventListener('DOMContentLoaded', function () {MasterUI.chart(document.getElementById('pages'), { type: 'bar', horizontal: true, values: true, label: 'Pages by visitors in the last 7 days', labels: ['Installation', 'Button', 'Dialog', 'Default theme', 'Command'], series: [{ name: 'Visitors', values: [4820, 3112, 2240, 1360, 412] }]})})</script><script type="module"> import { attach } from '@master/ui/behaviors' attach()</script>import { Chart } from './ui/chart'<div className="card max-w(520) mx-auto"> <div className="card-header"> <h3 className="card-title">Top pages</h3> <p className="card-description">Visitors in the last 7 days</p> </div> <Chart id="pages" style={{ '--chart-height': '11rem' } as React.CSSProperties} type="bar" horizontal values label="Pages by visitors in the last 7 days" labels={['Installation', 'Button', 'Dialog', 'Default theme', 'Command']} series={[{ name: 'Visitors', values: [4820, 3112, 2240, 1360, 412] }]} /></div><script> import Chart from './ui/Chart.svelte'</script><div class="card max-w(520) mx-auto"> <div class="card-header"> <h3 class="card-title">Top pages</h3> <p class="card-description">Visitors in the last 7 days</p> </div> <Chart id="pages" style="--chart-height: 11rem" type="bar" horizontal values label="Pages by visitors in the last 7 days" labels={['Installation', 'Button', 'Dialog', 'Default theme', 'Command']} series={[{ name: 'Visitors', values: [4820, 3112, 2240, 1360, 412] }]} /></div><script setup>import Chart from './ui/Chart.vue'</script><template> <div class="card max-w(520) mx-auto"> <div class="card-header"> <h3 class="card-title">Top pages</h3> <p class="card-description">Visitors in the last 7 days</p> </div> <Chart id="pages" style="--chart-height: 11rem" type="bar" horizontal values label="Pages by visitors in the last 7 days" :labels="['Installation', 'Button', 'Dialog', 'Default theme', 'Command']" :series="[{ name: 'Visitors', values: [4820, 3112, 2240, 1360, 412] }]" /> </div></template>#棒棒糖图与须线
mark: 'lollipop' 将每根柱画成一条末端带圆点的细线。它用更少的填充面积表达同样的数值,适合柱条较多或较长、实心柱条容易连成一片的图表。
棒棒糖图
<div class="card max-w(520) mx-auto"> <div class="card-header"> <h3 class="card-title">Response time</h3> <p class="card-description">Median by endpoint, in milliseconds</p> </div> <figure class="chart" id="endpoints" style="--chart-height: 11rem"></figure></div><script>addEventListener('DOMContentLoaded', function () {MasterUI.chart(document.getElementById('endpoints'), { type: 'bar', mark: 'lollipop', horizontal: true, values: true, label: 'Median response time by endpoint, in milliseconds', labels: ['/search', '/checkout', '/feed', '/profile', '/login'], series: [{ name: 'Median', values: [412, 318, 240, 132, 96] }]})})</script><script type="module"> import { attach } from '@master/ui/behaviors' attach()</script>import { Chart } from './ui/chart'<div className="card max-w(520) mx-auto"> <div className="card-header"> <h3 className="card-title">Response time</h3> <p className="card-description">Median by endpoint, in milliseconds</p> </div> <Chart id="endpoints" style={{ '--chart-height': '11rem' } as React.CSSProperties} type="bar" mark="lollipop" horizontal values label="Median response time by endpoint, in milliseconds" labels={['/search', '/checkout', '/feed', '/profile', '/login']} series={[{ name: 'Median', values: [412, 318, 240, 132, 96] }]} /></div><script> import Chart from './ui/Chart.svelte'</script><div class="card max-w(520) mx-auto"> <div class="card-header"> <h3 class="card-title">Response time</h3> <p class="card-description">Median by endpoint, in milliseconds</p> </div> <Chart id="endpoints" style="--chart-height: 11rem" type="bar" mark="lollipop" horizontal values label="Median response time by endpoint, in milliseconds" labels={['/search', '/checkout', '/feed', '/profile', '/login']} series={[{ name: 'Median', values: [412, 318, 240, 132, 96] }]} /></div><script setup>import Chart from './ui/Chart.vue'</script><template> <div class="card max-w(520) mx-auto"> <div class="card-header"> <h3 class="card-title">Response time</h3> <p class="card-description">Median by endpoint, in milliseconds</p> </div> <Chart id="endpoints" style="--chart-height: 11rem" type="bar" mark="lollipop" horizontal values label="Median response time by endpoint, in milliseconds" :labels="['/search', '/checkout', '/feed', '/profile', '/login']" :series="[{ name: 'Median', values: [412, 318, 240, 132, 96] }]" /> </div></template>为数据系列的每个标签各提供一个 low 和一个 high,即可表示数值的可能范围,例如均值附近的区间或多次运行结果的范围。须线穿过柱条末端,数值提示会在数值之后显示该区间。
须线
<div class="card max-w(440) mx-auto"> <div class="card-header"> <h3 class="card-title">Trial</h3> <p class="card-description">Mean score by group, with its interval</p> </div> <figure class="chart" id="trial" style="--chart-height: 11rem"></figure></div><script>addEventListener('DOMContentLoaded', function () {MasterUI.chart(document.getElementById('trial'), { type: 'bar', label: 'Mean score by group with its 95% interval', labels: ['Control', 'A', 'B', 'C'], series: [{ name: 'Score', values: [52, 61, 58, 70], low: [46, 55, 49, 64], high: [58, 67, 67, 76] }]})})</script><script type="module"> import { attach } from '@master/ui/behaviors' attach()</script>import { Chart } from './ui/chart'<div className="card max-w(440) mx-auto"> <div className="card-header"> <h3 className="card-title">Trial</h3> <p className="card-description">Mean score by group, with its interval</p> </div> <Chart id="trial" style={{ '--chart-height': '11rem' } as React.CSSProperties} type="bar" label="Mean score by group with its 95% interval" labels={['Control', 'A', 'B', 'C']} series={[{ name: 'Score', values: [52, 61, 58, 70], low: [46, 55, 49, 64], high: [58, 67, 67, 76] }]} /></div><script> import Chart from './ui/Chart.svelte'</script><div class="card max-w(440) mx-auto"> <div class="card-header"> <h3 class="card-title">Trial</h3> <p class="card-description">Mean score by group, with its interval</p> </div> <Chart id="trial" style="--chart-height: 11rem" type="bar" label="Mean score by group with its 95% interval" labels={['Control', 'A', 'B', 'C']} series={[{ name: 'Score', values: [52, 61, 58, 70], low: [46, 55, 49, 64], high: [58, 67, 67, 76] }]} /></div><script setup>import Chart from './ui/Chart.vue'</script><template> <div class="card max-w(440) mx-auto"> <div class="card-header"> <h3 class="card-title">Trial</h3> <p class="card-description">Mean score by group, with its interval</p> </div> <Chart id="trial" style="--chart-height: 11rem" type="bar" label="Mean score by group with its 95% interval" :labels="['Control', 'A', 'B', 'C']" :series="[{ name: 'Score', values: [52, 61, 58, 70], low: [46, 55, 49, 64], high: [58, 67, 67, 76] }]" /> </div></template>#多柱布局与叠加折线
各类别带平分绘图区的宽度,因此原本显示一周的空间也能放下一个月的数据。绘图区下方最多显示 12 个名称,宽度小于 24rem 时隔一个显示一个。设置了 type: 'line' 的数据系列会以折线叠绘在柱条上,并与柱条共用刻度,可用于显示目标值或移动平均值。两个指标需要不同的刻度时,必须分成两张图,不能使用双轴图。
30 天
<div class="card max-w(600) mx-auto"> <div class="card-header"> <h3 class="card-title">Requests</h3> <p class="card-description">Thousands per day in November, with the weekly average</p> </div> <figure class="chart" id="november"></figure></div><script>addEventListener('DOMContentLoaded', function () {var requests = [49, 52, 58, 61, 44, 37, 56, 60, 64, 72, 75, 68, 45, 42, 69, 73, 81, 76, 78, 49, 48, 72, 80, 85, 83, 86, 58, 52, 82, 88]var days = requests.map(function (value, index) { return 'Nov ' + (index + 1) })var average = requests.map(function (value, index) { var week = requests.slice(Math.max(0, index - 6), index + 1) return Math.round(week.reduce(function (sum, next) { return sum + next }, 0) / week.length)})MasterUI.chart(document.getElementById('november'), { type: 'bar', label: 'Requests per day in November, in thousands', labels: days, series: [ { name: 'Requests', values: requests }, { name: 'Weekly average', type: 'line', values: average, category: 'orange' } ]})})</script><script type="module"> import { attach } from '@master/ui/behaviors' attach()</script>import { useEffect, useRef } from 'react'import { attach, chart } from '@master/ui/behaviors'var requests = [49, 52, 58, 61, 44, 37, 56, 60, 64, 72, 75, 68, 45, 42, 69, 73, 81, 76, 78, 49, 48, 72, 80, 85, 83, 86, 58, 52, 82, 88]var days = requests.map(function (value, index) { return 'Nov ' + (index + 1) })var average = requests.map(function (value, index) { var week = requests.slice(Math.max(0, index - 6), index + 1) return Math.round(week.reduce(function (sum, next) { return sum + next }, 0) / week.length)})export function Example() { const root = useRef<HTMLDivElement>(null) useEffect(() => { const detach = [ attach(root.current!), chart(document.getElementById('november')!, { type: 'bar', label: 'Requests per day in November, in thousands', labels: days, series: [ { name: 'Requests', values: requests }, { name: 'Weekly average', type: 'line', values: average, category: 'orange' } ] }), ] return () => detach.forEach(stop => stop()) }, []) return ( <div ref={root} className="card max-w(600) mx-auto"> <div className="card-header"> <h3 className="card-title">Requests</h3> <p className="card-description">Thousands per day in November, with the weekly average</p> </div> <figure className="chart" id="november"></figure> </div> )}<script> import { attach, chart } from '@master/ui/behaviors' var requests = [49, 52, 58, 61, 44, 37, 56, 60, 64, 72, 75, 68, 45, 42, 69, 73, 81, 76, 78, 49, 48, 72, 80, 85, 83, 86, 58, 52, 82, 88] var days = requests.map(function (value, index) { return 'Nov ' + (index + 1) }) var average = requests.map(function (value, index) { var week = requests.slice(Math.max(0, index - 6), index + 1) return Math.round(week.reduce(function (sum, next) { return sum + next }, 0) / week.length) }) function mounted(root) { const detach = [ attach(root), chart(document.getElementById('november'), { type: 'bar', label: 'Requests per day in November, in thousands', labels: days, series: [ { name: 'Requests', values: requests }, { name: 'Weekly average', type: 'line', values: average, category: 'orange' } ] }), ] return () => detach.forEach(stop => stop()) }</script><div class="card max-w(600) mx-auto" {@attach mounted}> <div class="card-header"> <h3 class="card-title">Requests</h3> <p class="card-description">Thousands per day in November, with the weekly average</p> </div> <figure class="chart" id="november"></figure></div><script setup>import { onMounted, onUnmounted, ref } from 'vue'import { attach, chart } from '@master/ui/behaviors'var requests = [49, 52, 58, 61, 44, 37, 56, 60, 64, 72, 75, 68, 45, 42, 69, 73, 81, 76, 78, 49, 48, 72, 80, 85, 83, 86, 58, 52, 82, 88]var days = requests.map(function (value, index) { return 'Nov ' + (index + 1) })var average = requests.map(function (value, index) { var week = requests.slice(Math.max(0, index - 6), index + 1) return Math.round(week.reduce(function (sum, next) { return sum + next }, 0) / week.length)})const root = ref(null)let detach = []onMounted(() => { detach = [ attach(root.value), chart(document.getElementById('november'), { type: 'bar', label: 'Requests per day in November, in thousands', labels: days, series: [ { name: 'Requests', values: requests }, { name: 'Weekly average', type: 'line', values: average, category: 'orange' } ] }), ]})onUnmounted(() => detach.forEach(stop => stop()))</script><template> <div ref="root" class="card max-w(600) mx-auto"> <div class="card-header"> <h3 class="card-title">Requests</h3> <p class="card-description">Thousands per day in November, with the weekly average</p> </div> <figure class="chart" id="november"></figure> </div></template>#选项
| 选项 | 默认值 | |
|---|---|---|
label | 无 | 绘图区的无障碍名称,说明图表展示的内容及其单位 |
labels | [] | 沿坐标轴、雷达图四周或热力图各列上方显示的名称 |
series | [] | 每个数据系列包含 name 和图表类型所需的数据,可为 values、value、points、ranges 或 children,还可指定 category、type、low 和 high,以及环形图的 max |
stacked | false | 堆叠柱条或面积。'percent' 将每组堆叠归一化为 100%,'stream' 让堆叠面积围绕中线分布 |
horizontal | false | 将柱状图、区间图、哑铃图和箱线图横向绘制 |
mark | 'bar' | 'lollipop' 将柱条画成末端带圆点的细线 |
min, max, ticks | 自适应, 4 | 坐标轴的取值范围和大致的刻度数。散点图的横轴使用 xMin、xMax 和 xTicks |
axis | true | 是否显示刻度数值。设为 false 时只保留细网格线,适合卡片中的小图表 |
format, tickFormat | 随区域设置 | 数值和刻度标签的显示格式。散点图的横轴使用 xFormat |
dots, values | false | 是否标记折线上的每个数据点,以及是否显示每根柱条的数值 |
current | 无 | 标记了 aria-current 的数据点索引 |
legend, toggle | 有两个数据系列时显示, true | 是否显示图例,以及是否将图例项显示为复选框 |
各种图表共用同样的数值提示、键盘操作、图例、颜色和刻度机制。图表 文档列出了所有选项、类和自定义属性。