1- import { random } from "lodash"
21import { add , Duration } from 'date-fns'
32import { IStackedChartData } from "../src/types/chartData"
43
@@ -14,28 +13,60 @@ const numberFormatter = new Intl.NumberFormat('en-US', {
1413 style : 'percent'
1514} )
1615
17- export function generateData ( total = 1 , totalInStack = 5 , maxRandomValue = 1000 ) : IStackedChartData {
18- return Array . from ( { length : total } ) . map ( ( _ , idx ) => ( {
19- name : numberFormatter . format ( idx ) ,
20- color : colors [ idx % colors . length ] ,
21- data : Array . from ( { length : totalInStack } ) . map ( ( _ , idx ) => ( { metric : 10 * idx , value : random ( 0 , maxRandomValue ) , originalPayload : idx . toString ( ) } ) )
22- } ) )
16+ const FIXED_NOW = new Date ( Date . UTC ( 2026 , 0 , 1 , 12 , 0 , 0 ) ) ;
17+
18+ export const getStableValue = (
19+ maxValue : number ,
20+ pointIndex : number ,
21+ seriesIndex = 0 ,
22+ ) : number => {
23+ if ( maxValue <= 0 ) {
24+ return 0 ;
25+ }
26+
27+ return (
28+ ( ( pointIndex + 1 ) * 137 +
29+ ( seriesIndex + 1 ) * 71 +
30+ ( pointIndex + 1 ) * ( seriesIndex + 1 ) * 29 ) %
31+ ( maxValue + 1 )
32+ ) ;
33+ } ;
34+
35+ export function generateData (
36+ total = 1 ,
37+ totalInStack = 5 ,
38+ maxRandomValue = 1000 ,
39+ ) : IStackedChartData {
40+ return Array . from ( { length : total } ) . map ( ( _ , seriesIndex ) => ( {
41+ name : numberFormatter . format ( seriesIndex ) ,
42+ color : colors [ seriesIndex % colors . length ] ,
43+ data : Array . from ( { length : totalInStack } ) . map ( ( _ , pointIndex ) => ( {
44+ metric : 10 * pointIndex ,
45+ value : getStableValue ( maxRandomValue , pointIndex , seriesIndex ) ,
46+ originalPayload : pointIndex . toString ( ) ,
47+ } ) ) ,
48+ } ) ) ;
2349}
2450
25- const now = new Date ( )
26-
27- export function generateTimelineData ( total = 1 , totalInStack = 20 , maxRandomValue = 1000 , durationType : keyof Duration = 'days' , title ?: string ) : IStackedChartData {
28- return Array . from ( { length : total } ) . map ( ( _ , idx ) => ( {
29- name : numberFormatter . format ( idx ) ,
30- color : colors [ idx % colors . length ] ,
31- data : Array . from ( { length : totalInStack } ) . map ( ( _ , idx ) => {
32- const metric = add ( now , { [ durationType ] : idx } )
33-
34- return ( { metric,
35- value : random ( maxRandomValue ) ,
36- originalPayload : add ( now , { [ durationType ] : idx } ) ,
37- title : title && ( ( metric : string ) => `${ title } ${ metric } ` ) } )
38- } )
39- } )
40- )
51+ export function generateTimelineData (
52+ total = 1 ,
53+ totalInStack = 20 ,
54+ maxRandomValue = 1000 ,
55+ durationType : keyof Duration = 'days' ,
56+ title ?: string ,
57+ ) : IStackedChartData {
58+ return Array . from ( { length : total } ) . map ( ( _ , seriesIndex ) => ( {
59+ name : numberFormatter . format ( seriesIndex ) ,
60+ color : colors [ seriesIndex % colors . length ] ,
61+ data : Array . from ( { length : totalInStack } ) . map ( ( _ , pointIndex ) => {
62+ const metric = add ( FIXED_NOW , { [ durationType ] : pointIndex } ) ;
63+
64+ return {
65+ metric,
66+ value : getStableValue ( maxRandomValue , pointIndex , seriesIndex ) ,
67+ originalPayload : metric ,
68+ title : title && ( ( metric : string ) => `${ title } ${ metric } ` ) ,
69+ } ;
70+ } ) ,
71+ } ) ) ;
4172}
0 commit comments