graphieros / vue-data-ui

An open source user-empowering data visualization Vue 3 components library for eloquent data storytelling
https://vue-data-ui.graphieros.com/
MIT License
1.01k stars 42 forks source link

how to customize chart labels #24

Closed sombriks closed 6 months ago

sombriks commented 6 months ago

i am making use of prefix and suffix but i still present the percentage.

is there a way to present another value (i have the absolute value, would like to present it instead of the percentage) in lables?

using VueUiSparkbar.

this library is awesome.

thanks for any guidance.

sombriks commented 6 months ago

Ok i got a workaround:


const receitaDespesaTotalPeriodo = computed(() => {
  const total = dashboardState.store.dashboard.receitaDespesaTotalPeriodo
    .reduce((acc, e) => {
      acc += e.value
      return acc
    }, 0)
  return dashboardState.store.dashboard.receitaDespesaTotalPeriodo.map(r => ({
    ...r,
    name: `${r.label} ${prepareMoney(r.value)}`,
    value: 100 * r.value / total,
  }))
})

and styles.labels.value.show=false in config.

graphieros commented 6 months ago

Hey:)

I'm glad you found a workaround.

However there is a way to make it work natively:

  1. config
  1. dataset

Here is an example:

<script setup>
    import { computed } from "vue";
    import { VueUiSparkbar } from "vue-data-ui";
    import "vue-data-ui/style.css"

    const config = computed(() => {
        return {
     "style": {
    "backgroundColor": "#FFFFFF",
    "fontFamily": "inherit",
    "layout": {
      "independant": true,
      "percentage": false,
      "target": 1050
    },
    "gutter": {
      "backgroundColor": "#e1e5e8",
      "opacity": 100
    },
    "bar": {
      "gradient": {
        "show": true,
        "intensity": 40,
        "underlayerColor": "#FFFFFF"
      }
    },
    "gap": 4,
    "animation": {
      "show": true,
      "animationFrames": 60
    },
    "labels": {
      "fontSize": 16,
      "name": {
        "position": "top",
        "width": "100%",
        "color": "#1A1A1A",
        "bold": true
      },
      "value": {
        "show": true,
        "bold": true
      }
    }
  }
}
    })

    const dataset = computed(() => {
        return [
  {
    "name": "KPI 1",
    "value": 600,
    "color": "#6376DD",
    "prefix": "$",
    "suffix": "",
    "rounding": 1
  },
  {
    "name": "KPI 2",
    "value": 450,
    "color": "#42d392",
    "prefix": "$",
    "suffix": "",
    "rounding": 1
  }
]
    })

</script>

<template>
    <div style="width:600px">
        <VueUiSparkbar :config="config" :dataset="dataset" />
    </div>
</template>

Cheers

sombriks commented 6 months ago

Hey, thanks a lot for that, will use that way, it simplifies many of my dashboard computed values!

sombriks commented 6 months ago

And id got gorgeous! Thanks again for the guidance!

const receitaDespesaBarConfig = computed(() => {
  const total = dashboardState.store.dashboard.receitaDespesaTotalPeriodo
    .reduce((acc, e) => {
      acc += e.value
      return acc
    }, 0)
  return {
    style: {
      ...sparkBarConfig.style,
      layout: {
        ...sparkBarConfig.style.layout,
        independant: true,
        percentage: false,
        target: total
      }
    }
  }
})

const receitaDespesaTotalPeriodo = computed(() => {
  return dashboardState.store.dashboard.receitaDespesaTotalPeriodo.map(r => ({
    ...r,
    name: r.label,
    prefix: "R$ "
  }))
})
graphieros commented 6 months ago

You're welcome ! That's neat indeed.

The docs should probably have examples of such use cases, as it is not intuitive to toggle these config options to achieve this kind of result.

Thanks for using the lib, and for reaching out:)