Skip to content

Commit 5a29e35

Browse files
committed
feat: add simple legend formatting
1 parent 55cb952 commit 5a29e35

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

src/App.vue

+4-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@
3838
<input type="checkbox" v-model="circles[3].loading" />
3939
</div>-->
4040
<div style="border: 1px solid red; display: inline-block">
41-
<ve-progress :progress="progress" animation="rs 2000 2000" :legend-formatter="customFormatter"> </ve-progress>
41+
<!-- <ve-progress :progress="progress" animation="rs 2000 2000" :legend-formatter="customFormatter">
42+
</ve-progress>-->
43+
<ve-progress :progress="progress" animation="default 2500 1000" legend="0001239,5465"></ve-progress>
4244
</div>
4345
</div>
4446
</div>
@@ -142,7 +144,7 @@ export default {
142144
return new Intl.NumberFormat("de-DE", { style: "currency", currency: "EUR" }).format(value);
143145
},
144146
customFormatter(c) {
145-
console.log(c);
147+
// console.log(c);
146148
const f = "0000";
147149
const f2 = "00";
148150
const cv = (c.currentRawValue || 0.0).toFixed(2).toString().split(".");

src/components/Counter.vue

+17-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
</template>
66

77
<script>
8+
import { isString } from "../utils";
9+
810
export default {
911
name: "Counter",
1012
props: {
@@ -50,9 +52,18 @@ export default {
5052
return this.duration === 0 ? this.difference : this.difference / this.duration;
5153
},
5254
delimiter() {
53-
return this.value.toString().search(",") >= 0 ? "," : ".";
55+
return this.value.toString().includes(",") ? "," : ".";
5456
},
5557
formattedValue() {
58+
if (isString(this.value)) {
59+
let [preFormat] = this.value.toString().replace(/\s/g, "").split(this.delimiter);
60+
preFormat = [...preFormat].fill("0").join("");
61+
const [pre, post] = this.currentValue
62+
.toFixed(this.decimalsCount)
63+
.replace(".", this.delimiter)
64+
.split(this.delimiter);
65+
return `${preFormat.slice(pre.length)}${pre}${post ? this.delimiter + post : ""}`;
66+
}
5667
return this.currentValue.toFixed(this.countDecimals()).replace(".", this.delimiter);
5768
},
5869
delay() {
@@ -64,6 +75,10 @@ export default {
6475
countProgress() {
6576
return (Math.abs(this.currentDifference - this.difference) * 100) / (this.difference || 1);
6677
},
78+
decimalsCount() {
79+
if (!isString(this.value) && this.value % 1 === 0) return 0;
80+
return this.value.toString().replace(/\s/g, "").split(this.delimiter)[1].length;
81+
},
6782
counterProps() {
6883
return {
6984
currentValue: parseFloat(this.formattedValue),
@@ -85,7 +100,7 @@ export default {
85100
methods: {
86101
countDecimals() {
87102
if (this.value % 1 === 0) return 0;
88-
return this.value.toString().split(this.delimiter)[1].length;
103+
return this.value.toString().replace(/\s/g, "").split(this.delimiter)[1].length;
89104
},
90105
count(timeStamp) {
91106
if (!this.startTime) {

src/utils.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const isValidNumber = (prop) => prop !== undefined && prop !== "" && prop !== null && !Number.isNaN(parseFloat(prop));
2+
export const isString = (prop) => typeof prop === "string" || prop instanceof String;
23

34
const getNumberIfValid = (prop) => {
45
if (isValidNumber(prop)) {

0 commit comments

Comments
 (0)