[READ-ONLY] Mirror of https://github.com/probablykasper/starchart. GitHub star history graph starchart.kasper.space
chart github github-api github-star graph history star star-history stargazers stars
0

Configure Feed

Select the types of activity you want to include in your feed.

Fix aligned time labels

Kasper (Mar 14, 2023, 12:53 PM +0100) 7298ba60 1a1729db

+43 -34
+43 -34
src/routes/chart.ts
··· 129 129 } 130 130 setDefaultFormatting() 131 131 132 + function setAlignedFormatting() { 133 + const startDate = new Date(2001, 0) 134 + const day = 1000 * 3600 * 24 135 + const startDay = Math.ceil(startDate.getTime() / day) 136 + chart.instance.applyOptions({ 137 + localization: { 138 + timeFormatter: (time: BusinessDay) => { 139 + const date = new Date(time.year, time.month - 1, time.day) 140 + return Math.ceil(date.getTime() / day) - startDay + ' days' 141 + }, 142 + }, 143 + timeScale: { 144 + tickMarkFormatter: (time: BusinessDay, tickMarkType: TickMarkType) => { 145 + const date = new Date(time.year, time.month - 1, time.day) 146 + if (tickMarkType === TickMarkType.DayOfMonth) { 147 + return Math.ceil(date.getTime() / day) - startDay + 'd' 148 + } 149 + const years = date.getFullYear() - startDate.getFullYear() 150 + if (tickMarkType === TickMarkType.Month) { 151 + return date.getMonth() - startDate.getMonth() + 12 * years + 'm' 152 + } else if (tickMarkType === TickMarkType.Year) { 153 + switch (years) { 154 + case 0: 155 + return '0d' 156 + case 1: 157 + return '1y' 158 + default: 159 + return years + 'y' 160 + } 161 + } else { 162 + return null 163 + } 164 + }, 165 + }, 166 + }) 167 + } 168 + if (chart.align) { 169 + setAlignedFormatting() 170 + } else { 171 + setDefaultFormatting() 172 + } 173 + 132 174 const fillerLine = chart.instance.addAreaSeries({ 133 175 priceScaleId: '', 134 176 visible: false, ··· 197 239 line.instance.setData(alignedChartSeries) 198 240 line.lastChartSeriesDate = alignedChartSeries[alignedChartSeries.length - 1].time 199 241 } 200 - const startDate = new Date(2001, 0) 201 - const day = 1000 * 3600 * 24 202 - const startDay = Math.ceil(startDate.getTime() / day) 203 - chart.instance.applyOptions({ 204 - localization: { 205 - timeFormatter: (time: BusinessDay) => { 206 - const date = new Date(time.year, time.month - 1, time.day) 207 - return Math.ceil(date.getTime() / day) - startDay + ' days' 208 - }, 209 - }, 210 - timeScale: { 211 - tickMarkFormatter: (time: BusinessDay, tickMarkType: TickMarkType) => { 212 - const date = new Date(time.year, time.month - 1, time.day) 213 - if (tickMarkType === TickMarkType.DayOfMonth) { 214 - return Math.ceil(date.getTime() / day) - startDay + 'd' 215 - } 216 - const years = date.getFullYear() - startDate.getFullYear() 217 - if (tickMarkType === TickMarkType.Month) { 218 - return date.getMonth() - startDate.getMonth() + 12 * years + 'm' 219 - } else if (tickMarkType === TickMarkType.Year) { 220 - switch (years) { 221 - case 0: 222 - return '0d' 223 - case 1: 224 - return '1y' 225 - default: 226 - return years + 'y' 227 - } 228 - } else { 229 - return null 230 - } 231 - }, 232 - }, 233 - }) 242 + setAlignedFormatting() 234 243 updateFiller(chart.lines) 235 244 this.resetZoom() 236 245 set(chart)