|
1 | 1 | import { |
| 2 | + Calendar, |
2 | 3 | Dataset, |
| 4 | + Geo, |
3 | 5 | Legend, |
4 | 6 | LineChart, |
5 | 7 | PieChart, |
| 8 | + ScatterChart, |
6 | 9 | Title, |
7 | 10 | Toolbox, |
8 | 11 | Tooltip, |
9 | 12 | VisualMap, |
10 | 13 | echarts, |
11 | 14 | } from '@fanciers/echarts-react'; |
12 | 15 | import type { Meta, StoryObj } from '@storybook/react'; |
| 16 | +import type { PieSeriesOption } from 'echarts/charts'; |
13 | 17 | import React from 'react'; |
| 18 | +import useSWR from 'swr'; |
14 | 19 |
|
15 | 20 | const meta = { |
16 | 21 | title: 'Pie', |
@@ -694,6 +699,144 @@ export const DatasetDefault: Story = { |
694 | 699 | }, |
695 | 700 | }; |
696 | 701 |
|
| 702 | +export const MapIcelandPie: Story = { |
| 703 | + name: 'Pie Charts on GEO Map', |
| 704 | + render() { |
| 705 | + const { data: geoJSON } = useSWR('https://echarts.apache.org/examples/data/asset/geo/iceland.geo.json'); |
| 706 | + const [isGeoLoaded, setIsGeoLoaded] = React.useState(false); |
| 707 | + React.useEffect(() => { |
| 708 | + if (!geoJSON) return; |
| 709 | + echarts.registerMap('iceland', geoJSON); |
| 710 | + setIsGeoLoaded(true); |
| 711 | + }, [geoJSON]); |
| 712 | + |
| 713 | + function randomPieSeries(center: [number, number] | string, radius: number): PieSeriesOption { |
| 714 | + const data = ['A', 'B', 'C', 'D'].map((t) => { |
| 715 | + return { value: Math.round(Math.random() * 100), name: 'Category ' + t }; |
| 716 | + }); |
| 717 | + return { |
| 718 | + type: 'pie', |
| 719 | + coordinateSystem: 'geo', |
| 720 | + tooltip: { formatter: '{b}: {c} ({d}%)' }, |
| 721 | + label: { show: false }, |
| 722 | + labelLine: { show: false }, |
| 723 | + animationDuration: 0, |
| 724 | + radius, |
| 725 | + center, |
| 726 | + data, |
| 727 | + }; |
| 728 | + } |
| 729 | + |
| 730 | + return ( |
| 731 | + <PieChart |
| 732 | + style={{ width: 480, height: 300 }} |
| 733 | + series={[ |
| 734 | + randomPieSeries([-19.007740346534653, 64.1780281585128], 45), |
| 735 | + randomPieSeries([-17.204666089108912, 65.44804833928391], 25), |
| 736 | + randomPieSeries([-15.264995297029705, 64.8592208009264], 30), |
| 737 | + randomPieSeries( |
| 738 | + // it's also supported to use geo region name as center since v5.4.1 |
| 739 | + +echarts.version.split('.').slice(0, 3).join('') > 540 |
| 740 | + ? 'Vestfirðir' |
| 741 | + : // or you can only use the LngLat array |
| 742 | + [-13, 66], |
| 743 | + 30 |
| 744 | + ), |
| 745 | + ]} |
| 746 | + > |
| 747 | + <Geo |
| 748 | + geo={ |
| 749 | + isGeoLoaded |
| 750 | + ? { |
| 751 | + map: 'iceland', |
| 752 | + roam: true, |
| 753 | + aspectScale: Math.cos((65 * Math.PI) / 180), |
| 754 | + // nameProperty: 'name_en', // If using en name. |
| 755 | + itemStyle: { areaColor: '#e7e8ea' }, |
| 756 | + emphasis: { label: { show: false } }, |
| 757 | + } |
| 758 | + : [] |
| 759 | + } |
| 760 | + /> |
| 761 | + <Tooltip tooltip={{}} /> |
| 762 | + <Legend legend={{}} /> |
| 763 | + </PieChart> |
| 764 | + ); |
| 765 | + }, |
| 766 | +}; |
| 767 | + |
| 768 | +export const CalendarPie: Story = { |
| 769 | + name: 'Calendar Pie', |
| 770 | + render() { |
| 771 | + const cellSize: [number, number] = [80, 80]; |
| 772 | + const pieRadius = 30; |
| 773 | + function getVirtualData() { |
| 774 | + const date = +echarts.time.parse('2017-02-01'); |
| 775 | + const end = +echarts.time.parse('2017-03-01'); |
| 776 | + const dayTime = 3600 * 24 * 1000; |
| 777 | + const data = []; |
| 778 | + for (let time = date; time < end; time += dayTime) { |
| 779 | + data.push([echarts.time.format(time, '{yyyy}-{MM}-{dd}', false), Math.floor(Math.random() * 10000)]); |
| 780 | + } |
| 781 | + return data; |
| 782 | + } |
| 783 | + const scatterData = getVirtualData(); |
| 784 | + const pieSeries = scatterData.map( |
| 785 | + (item, index): PieSeriesOption => ({ |
| 786 | + type: 'pie', |
| 787 | + id: `pie-${index}`, |
| 788 | + center: item[0]!, |
| 789 | + radius: pieRadius, |
| 790 | + coordinateSystem: 'calendar', |
| 791 | + label: { formatter: '{c}', position: 'inside' }, |
| 792 | + data: [ |
| 793 | + { name: 'Work', value: Math.round(Math.random() * 24) }, |
| 794 | + { name: 'Entertainment', value: Math.round(Math.random() * 24) }, |
| 795 | + { name: 'Sleep', value: Math.round(Math.random() * 24) }, |
| 796 | + ], |
| 797 | + }) |
| 798 | + ); |
| 799 | + |
| 800 | + return ( |
| 801 | + <PieChart |
| 802 | + compose={[ScatterChart]} |
| 803 | + style={{ width: 600, height: 500 }} |
| 804 | + series={[ |
| 805 | + { |
| 806 | + id: 'label', |
| 807 | + type: 'scatter', |
| 808 | + coordinateSystem: 'calendar', |
| 809 | + symbolSize: 0, |
| 810 | + label: { |
| 811 | + show: true, |
| 812 | + formatter: (params) => echarts.time.format((params.value as [string, number])[0], '{dd}', false), |
| 813 | + offset: [-cellSize[0] / 2 + 10, -cellSize[1] / 2 + 10], |
| 814 | + fontSize: 14, |
| 815 | + }, |
| 816 | + data: scatterData, |
| 817 | + }, |
| 818 | + ...pieSeries, |
| 819 | + ]} |
| 820 | + > |
| 821 | + <Tooltip tooltip={{}} /> |
| 822 | + <Legend legend={{ data: ['Work', 'Entertainment', 'Sleep'], bottom: 20 }} /> |
| 823 | + <Calendar |
| 824 | + calendar={{ |
| 825 | + top: 'middle', |
| 826 | + left: 'center', |
| 827 | + orient: 'vertical', |
| 828 | + cellSize: cellSize, |
| 829 | + yearLabel: { show: false, fontSize: 30 }, |
| 830 | + dayLabel: { margin: 20, firstDay: 1, nameMap: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'] }, |
| 831 | + monthLabel: { show: false }, |
| 832 | + range: ['2017-02'], |
| 833 | + }} |
| 834 | + /> |
| 835 | + </PieChart> |
| 836 | + ); |
| 837 | + }, |
| 838 | +}; |
| 839 | + |
697 | 840 | export const DatasetLink: Story = { |
698 | 841 | name: 'Share Dataset', |
699 | 842 | render() { |
|
0 commit comments