|
| 1 | +<script setup> |
| 2 | +import { VVPlot, VVAxisX, VVAxisY, VVGeomLine, VVGeomBar, VVGeomPoint, VVAction } from '#base/components' |
| 3 | +import vvbreak from '#base/js/break' |
| 4 | +import vvlabel from '#base/js/label' |
| 5 | +
|
| 6 | +import letters_txt from './data/sentences.txt?raw' |
| 7 | +const letters = letters_txt.toLowerCase().replace(/[^a-z]/g, "").split("") |
| 8 | +import economics_txt from './data/economics.csv?raw' |
| 9 | +const economics = parse_csv(economics_txt) |
| 10 | +
|
| 11 | +function parse_csv(text) { |
| 12 | + function parse_value(v) { |
| 13 | + if (!isNaN(v)) return +v |
| 14 | + if (/^\d{4}-\d{1,2}-\d{1,2}$/.test(v)) { |
| 15 | + let [y, m, d] = v.split('-').map(x => +x) |
| 16 | + return Date.UTC(y, m - 1, d) |
| 17 | + } |
| 18 | + return v |
| 19 | + } |
| 20 | + let [header, ...lines] = text.replace(/\r/g, '').split('\n').filter(l => l) |
| 21 | + header = header.split(',') |
| 22 | + return lines.map(l => Object.fromEntries(l.split(',').map((v, i) => [header[i], parse_value(v)]))) |
| 23 | +} |
| 24 | +</script> |
| 25 | +<template> |
| 26 | + <div class="plot-container"> |
| 27 | + <pre class="code">{{`<VVPlot :data="economics" :theme="{ plot: { margin_right: 50 } }"> |
| 28 | + <VVAxisY :labels="v => \`\${v * 100}%\`" :expand-mult="{ min: 0.2, max: 0.1 }" title="unemployment rate" |
| 29 | + :theme="{ title_color: 'gray' }"> |
| 30 | + <VVAction move rescale zoom /> |
| 31 | + </VVAxisY> |
| 32 | + <VVAxisX position="10%" |
| 33 | + :theme="{ ticks_length: 3, title_position: 'right', title_size: 16, text_angle: 45, ticks_anchor_x: 0 }" |
| 34 | + :show-grid="false" title="date" :extend="0.5"> |
| 35 | + <VVAction move rescale zoom /> |
| 36 | + </VVAxisX> |
| 37 | + <VVGeomLine :x="d => new Date(d.date)" :y="d => d.unemploy / d.pop" /> |
| 38 | +</VVPlot>`}}</pre> |
| 39 | + <VVPlot :data="economics" :theme="{ plot: { margin_right: 50 } }"> |
| 40 | + <VVAxisY :labels="v => `${v * 100}%`" :expand-mult="{ min: 0.2, max: 0.1 }" title="unemployment rate" |
| 41 | + :theme="{ title_color: 'gray' }"> |
| 42 | + <VVAction move rescale zoom /> |
| 43 | + </VVAxisY> |
| 44 | + <VVAxisX position="10%" |
| 45 | + :theme="{ ticks_length: 3, title_position: 'right', title_size: 16, text_angle: 45, ticks_anchor_x: 0 }" |
| 46 | + :show-grid="false" title="date" :extend="0.5"> |
| 47 | + <VVAction move rescale zoom /> |
| 48 | + </VVAxisX> |
| 49 | + <VVGeomLine :x="d => new Date(d.date)" :y="d => d.unemploy / d.pop" /> |
| 50 | + </VVPlot> |
| 51 | + <hr> |
| 52 | + <pre class="code">{{`<VVPlot :data="economics"> |
| 53 | + <VVAxisX :labels="vvlabel.timestamp({ format: 'yyyy/MM' })" position="top" /> |
| 54 | + <VVAxisY position="left" primary /> |
| 55 | + <VVAxisY position="right" /> |
| 56 | + <VVGeomLine :x="d => new Date(d.date)" :y="d => d.pop" /> |
| 57 | +</VVPlot>`}}</pre> |
| 58 | + <VVPlot :data="economics"> |
| 59 | + <VVAxisX :labels="vvlabel.timestamp({ format: 'yyyy/MM' })" position="top" /> |
| 60 | + <VVAxisY position="left" primary /> |
| 61 | + <VVAxisY position="right" /> |
| 62 | + <VVGeomLine :x="d => new Date(d.date)" :y="d => d.pop" /> |
| 63 | + </VVPlot> |
| 64 | + <hr> |
| 65 | + <pre class="code">{{`<VVPlot :data="economics"> |
| 66 | + <VVAxisX :labels="vvlabel.timestamp()" :extend="2" :position="0" /> |
| 67 | + <VVAxisY :labels="vvlabel.default()" :expand-mult="0" /> |
| 68 | + <VVGeomLine :x="d => new Date(d.date)" :y="(d, i, data) => d.unemploy - (data[i - 1] ?? d)?.unemploy" color="#ccc" /> |
| 69 | +</VVPlot>`}}</pre> |
| 70 | + <VVPlot :data="economics"> |
| 71 | + <VVAxisX :labels="vvlabel.timestamp()" :extend="2" :position="0" /> |
| 72 | + <VVAxisY :labels="vvlabel.default()" :expand-mult="0" /> |
| 73 | + <VVGeomLine :x="d => new Date(d.date)" :y="(d, i, data) => d.unemploy - (data[i - 1] ?? d)?.unemploy" |
| 74 | + color="#ccc" /> |
| 75 | + </VVPlot> |
| 76 | + <hr> |
| 77 | + <pre class="code">{{`<VVPlot :data="[{ x: 2, y: -1 }, { x: -1, y: 2 }]"> |
| 78 | + <VVAxisX :position="0" :expand-mult="1" :breaks="vvbreak.number({ step: 1 })"> |
| 79 | + <VVAction move rescale zoom /> |
| 80 | + </VVAxisX> |
| 81 | + <VVAxisY :position="0" :expand-mult="1" :extend="1"> |
| 82 | + <VVAction move rescale zoom /> |
| 83 | + </VVAxisY> |
| 84 | + <VVGeomPoint :x="d => d.x" :y="d => d.y" /> |
| 85 | +</VVPlot>`}}</pre> |
| 86 | + <VVPlot :data="[{ x: 2, y: -1 }, { x: -1, y: 2 }]"> |
| 87 | + <VVAxisX :position="0" :expand-mult="1" :breaks="vvbreak.number({ step: 1 })"> |
| 88 | + <VVAction move rescale zoom /> |
| 89 | + </VVAxisX> |
| 90 | + <VVAxisY :position="0" :expand-mult="1" :extend="1"> |
| 91 | + <VVAction move rescale zoom /> |
| 92 | + </VVAxisY> |
| 93 | + <VVGeomPoint :x="d => d.x" :y="d => d.y" /> |
| 94 | + </VVPlot> |
| 95 | + <hr> |
| 96 | + <pre class="code">{{`<VVPlot :data="letters"> |
| 97 | + <VVAxisX :expand-add="1" :expand-mult="0" :levels="['x', 'y', 'z', '', 'a', 'b', 'c']" :position="0" /> |
| 98 | + <VVAxisY position="center" :theme="{ ticks_anchor_y: 1 }" /> |
| 99 | + <VVGeomBar :x="d => d" /> |
| 100 | +</VVPlot>`}}</pre> |
| 101 | + <VVPlot :data="letters"> |
| 102 | + <VVAxisX :expand-add="1" :expand-mult="0" :levels="['x', 'y', 'z', '', 'a', 'b', 'c']" :position="0" /> |
| 103 | + <VVAxisY position="center" :theme="{ ticks_anchor_y: 1 }" /> |
| 104 | + <VVGeomBar :x="d => d" /> |
| 105 | + </VVPlot> |
| 106 | + </div> |
| 107 | +</template> |
| 108 | +<style scoped> |
| 109 | +.vvplot { |
| 110 | + width: 600px; |
| 111 | + height: 400px; |
| 112 | +} |
| 113 | +</style> |
0 commit comments