Highcharts-vue options as variable - vue.js

I want to replicate the same chart several times, using different data just like here https://jsfiddle.net/BlackLabel/qndeho5u/.
The concept works quite well but when transporting it to Vue there's something going wrong as the parameters aren't copied. For example polar isn't taken by the chart.
To avoid writing the chart options all the time I save it as a variable. On the jsfiddle it works but on my Vue code it doesn't.
What am I missing here?
<template>
<v-container fluid text-xs-center style="height: 90vh; max-height: 100%;">
<v-layout row wrap>
<v-flex xs6>
<v-card flat>
<Chart :options="chart1"/>
</v-card>
</v-flex>
<v-flex xs6>
<v-card flat>
<Chart :options="chart2"/>
</v-card>
</v-flex>
</v-layout>
</v-container>
</template>
<script>
import Chart from '../components/Chart.vue'
import Highcharts from 'highcharts'
var testdata2 = {name:'a', value: [
// each slice of the pie gets its own color
{ y: 10, color: '#1DACE8', name: 'a' },
{ y: 30, color: '#1C366B', name: 'b' },
{ y: 45, color: '#F24D29', name: 'c' },
{ y: 93, color: '#E5C4A1', name: 'd' },
{ y: 15, color: '#C4CFD0', name: 'e' }
]}
var testdata3 = {name:'b', value:[
// each slice of the pie gets its own color
{ y: 30, color: '#1DACE8', name: '1a' },
{ y: 20, color: '#1C366B', name: '2b' },
{ y: 35, color: '#F24D29', name: '3c' },
{ y: 23, color: '#E5C4A1', name: '4d' },
{ y: 95, color: '#C4CFD0', name: '5e' }
]}
var options = {
chart: {
polar: true,
backgroundColor: '#F5F5F5'
},
credits: {
enabled: false
},
exporting: {
buttons: {
contextButton: {
theme: {
fill: "#F5F5F5"
}
}
}
},
title: {
text: 'City 1'
},
subtitle: {
text: 'Railacc stuff'
},
pane: {
startAngle: -22.5
},
xAxis: {
tickInterval: 45,
min: 0,
max: 360,
labels: false
},
plotOptions: {
series: {
pointStart: 0,
pointInterval: 45
},
column: {
pointPadding: 0,
groupPadding: 0,
colorByPoint: true
}
},
tooltip: {
formatter: function() {
return this.y, this.point.name;
}
}
}
export default {
name: 'chartapp2',
components: {
Chart,
},
data() {
return {
chart1:{
options,
legend: {
enabled: false
},
series: [{
type: 'column',
data: testdata2.value,
pointPlacement: 'between'
}]
},
chart2:{
options,
legend: {
enabled: false
},
series: [{
type: 'column',
data: testdata3.value,
pointPlacement: 'between'
}]
},
}
},
}
</script>

I have reproduced this chart for you in Vue framework. Take a look at it and let me know if something is not clear for you.
Demo:
https://codesandbox.io/s/vue-template-9xb0c?fontsize=14

Related

Vue apex charts | Legend markers not aligned vertically

Using apex chart's vue wrapper
I have a donut chart:
<v-row justify="center">
<apexchart
width="500"
type="donut"
:options="options"
:series="donutSeries"
></apexchart>
</v-row>
Chart options:
options: {
legend: {
position: 'right',
},
labels: ['a', 'b', 'c', 'd', 'e'], // Name
dataLabels: {
enabled: false,
},
plotOptions: {
donut: {
labels: {
show: true,
name: {
show: true,
fontSize: '22px',
fontFamily: 'Comfortaa',
color: '#dfsda',
offsetY: -10
},
value: {
show: true,
fontSize: '16px',
fontFamily: 'Comfortaa',
color: undefined,
offsetY: 16,
formatter: function (val) {
return '$' + val
}
},
}
}
},
tooltip: {
enabled: true,
y: {
formatter: function (val) {
return '$' + val
}
},
x: {
formatter: function (val) {
return '$' + val
}
}
},
},
The labels for the 'Name' value which is displaying on the legend on the right side of the chart. It is not vertically aligned properly. Like in .
I tried to change legend positioning attributes and nothing changed. https://apexcharts.com/docs/options/legend/

Remove empty space around a barchart with no axes showing in Chartjs

Fairly straightforward, but I have a single bar on a barchart that I want to show by itself. I've hidden the axes and their ticks, but there is still all of that space there on the left and top//bottom of chart from the axes and the legend I am presuming.
Is there a way to remove this space without just putting negative margins on the element?
I've tried working with negative padding, but that doesn't work on the top and bottom of the chart.
I made a quick CodeSandbox to show my actual component
Any tips would be greatly appreciated!
Cheers!
Horizontal Stacked Bar Chart
<template>
<div style="width: 100%; height: 100%">
<canvas :id="id" width="100%" height="100%"></canvas>
</div>
</template>
<script>
import { defineComponent, onMounted } from "vue"
import Chart from "chart.js/auto"
import ChartDataLabels from "chartjs-plugin-datalabels"
export default defineComponent({
props: {
kind: {
/**
* #example: 'single', 'stacked'
*/
type: String,
default: "stacked",
},
color: {
type: String,
default: "rgba(255, 99, 132, 1)",
},
labels: {
type: Array,
default: () => ["S1", "S2", "S4", "S6"],
},
datasets: {
type: Array,
default: () => [
{
label: "va_value",
data: [80, 10, 60, 50],
backgroundColor: "#11DCB5",
borderColor: "transparent",
barThickness: 20,
minBarLength: 4,
// This is here to show SOMETHING even if the value is 0
},
{
label: "nva_value",
data: [20, 0, 40, 0],
backgroundColor: "#CA2F4B",
borderColor: "transparent",
barThickness: 20,
minBarLength: 4,
},
],
},
},
setup(props, context) {
const id = Math.random().toString()
onMounted(() => {
Chart.register(ChartDataLabels)
const ctx = document.getElementById(id)
const myChart = new Chart(ctx, {
type: "bar",
data: {
labels: props.labels,
datasets: props.datasets,
},
options: {
layout: {
padding: {
left: -20,
bottom: -20,
top: -20
}
},
plugins: {
legend: {
display: false,
},
datalabels: {
formatter: function (value) {
return props.kind === "stacked" ? `${value}s` : `${value}%`
},
color: "#fff",
anchor: "center",
align: "center",font: {
weight: "bold",
},
},
},
responsive: true,
maintainAspectRatio: false,
indexAxis: "y",
scales: {
y: {
beginAtZero: true,
stacked: true,
ticks: {
color: "#fff",
},
grid: {
drawBorder: false,
display: false,
},
},
x: {
display: props.kind === "stacked" ? true : false,
beginAtZero: true,
stacked: true,
ticks: {
color: "#fff",
},
grid: {
drawBorder: false,
display: false,
},
title: {
display: true,
text: props.kind === "stacked" ? "Step Time (s)" : "",
color: "#fff",
},
},
},
},
})
myChart
})
return { id }
},
})
</script>
<style lang=""></style>
This is because you set barThickness to 20 in your datasets. If you remove this it will show as you want:
https://codesandbox.io/s/modest-wave-60xui0

Cannot change anything in chart options

I have a bar chart and I would like to change the font color, border width and some other tings, but it doesn't work. It is in my computed property. In the chartOptions I want to change the y axis min and max value but I don't know if it is correct. Can anyone help me?
Also I want to make a horizontal line in this bar chart. It is the "Enemy's Avarage" and now it is a constant. I set the type to line and now I have a dot in my chart.
This is my chart component:
<script>
import { defineComponent } from 'vue'
import { Bar } from 'vue3-chart-v2'
export default defineComponent({
name: 'ChanceChart',
extends: Bar,
props: {
chartData: {
type: Object,
required: true
},
chartOptions: {
type: Object,
required: false,
},
},
mounted () {
this.renderChart(this.chartData, this.chartOptions)
}
})
</script>
And this is my app:
<template>
<div id="chart">
<ChanceChart :chartData="chartData" :chartOptions="chartOptions" />
</div>
</template>
<script>
import Navigation from "../components/Navigation.vue";
import ChanceChart from "../components/ChanceChart.vue";
import PageLoader from "../components/PageLoader.vue";
export default {
components: {
ChanceChart,
},
computed: {
chartOptions() {
return {
options: {
scales: {
y: {
title: {
display: true,
text: 'Value'
},
min: 0,
max: 100,
ticks: {
stepSize: 10,
}
},
},
elements: {
point: {
radius: 0
},
line: {
borderWidth: 2
}
},
plugins: {
legend: {
labels: {
boxWidth: 0,
font: {
fontColor: "#fff",
color: "#fff"
},
},
},
},
}
}
},
chartData() {
return {
datasets: [
{
type: 'bar',
label: "Enemy's Chance",
borderColor: "#1161ed",
borderWidth: 2,
data: this.emnemyCardsFilled,
},
{
type: 'bar',
label: "My Chance",
borderColor: "#f87979",
borderWidth: 2,
data: this.myCardsFilled,
},
{
type: 'line',
label: "Enemy's Avarage",
borderColor: "rgb(238, 255, 0)",
borderWidth: 2,
data: [50],
},
],
}
},
},
You need to remove the options part in the computed chartOptions prop:
chartOptions() {
return {
scales: {
y: {
title: {
display: true,
text: 'Value'
},
min: 0,
max: 100,
ticks: {
stepSize: 10,
}
},
},
elements: {
point: {
radius: 0
},
line: {
borderWidth: 2
}
},
plugins: {
legend: {
labels: {
boxWidth: 0,
font: {
fontColor: "#fff",
color: "#fff"
},
},
},
},
}
},

ApexCharts Donut Chart legend titles not reflecting provided data labels (stuck as 'series-1' etc.)

I'm importing Apexcharts' simple donut chart to my Vue.js project, however, even after following the documentation provided on their site, the legend titles stay as 'series-1, series-2, ...'.
Here's a picture of what's rendered: Donut Chart
As you can see I'm following the documentation, adding series and labels to both the data object and the div element, but for some reason still can't render the proper labels.
How would I go about resolving this issue?
Side note: switching legend.show from false to true does nothing.
<div id="donut-chart">
<v-container>
<div id="chart">
<apexchart
type=donut
width=380
:options="chartOptions"
:labels="labels"
:series="series" />
</div>
</v-container>
</div>
import VueApexCharts from 'vue-apexcharts'
export default {
name: 'donut-chart',
data: () => ({
series: [23, 11, 54, 72, 12],
labels: ["Apple", "Mango", "Banana", "Papaya", "Orange"],
chartOptions: {
dataLabels: {
enabled: false
},
responsive: [{
breakpoint: 480,
options: {
chart: {
width: 200
},
legend: {
show: false
}
}
}],
legend: {
position: 'right',
offsetY: 0,
height: 230
}
}
}),
components: {
apexchart: VueApexCharts,
}
}
labels property should be a nested property of chartOptions, not to be passed as a separate prop.
chartOptions: {
labels: ["Apple", "Mango", "Banana", "Papaya", "Orange"],
dataLabels: {
enabled: false
},
responsive: [{
breakpoint: 480,
options: {
chart: {
width: 200
},
legend: {
show: false
}
}
}],
legend: {
position: 'right',
offsetY: 0,
height: 230
}
}
<apexchart
type=donut
width=380
:options="chartOptions"
:series="series" />

vue2-google-maps mark-icon size and use of the google object to resize don't work

I'm using vue2-google-maps and i'm trying change marker-icon size and use the google object to resize, but don't work when i refresh the page.
Here is the problem:
First attempt after save Working fine!
After I refresh the page Doesn't work
I try to emulate the exemple of use google object from the website of vue2-google-maps.
The HTML/Vue
<template>
<v-content class="background-light">
<v-layout row wrap>
<v-flex xs12 class="mb-4">
<GmapMap
:center="{lat: 19.636226, lng: 42.806212}"
:zoom="5"
map-type-id="terrain"
style="width: 500px; height: 300px"
>
<GmapMarker
:key="index"
v-for="(m, index) in markers"
:position="m.position"
:clickable="true"
:draggable="true"
#click="center=m.position"
:icon="m.icon"
:label="m.label"
/>
</GmapMap>
</v-flex>
</v-layout>
</v-container>
</v-content>
</template>
This the script
<script>
import {gmapApi} from 'vue2-google-maps'
export default {
data: () => ({
markers: [],
}),
created () {
this.initialize()
},
computed: {
google: gmapApi
},
methods: {
initialize () {
this.markers = [
{
position:
{
lat: 19.636226,
lng: 42.806212
},
icon: {
url: 'https://image.flaticon.com/teams/slug/google.jpg',
scaledSize: this.google && new google.maps.Size(28, 28),
labelOrigin: this.google && new google.maps.Point(16,-10),
},
title: 'title',
label: {
text: 'label',
color: "black",
fontWeight: "bold",
fontSize: "12px"
}
},
{
position:
{
lat: 18.445320,
lng: 47.989452
},
icon: {
url: 'https://image.flaticon.com/teams/slug/google.jpg',
scaledSize: this.google && new google.maps.Size(28, 28),
labelOrigin: this.google && new google.maps.Point(16,-10),
},
title: 'title',
label: {
text: 'label',
color: "black",
fontWeight: "bold",
fontSize: "12px"
}
},
{
position:
{
lat: 19.128580,
lng: 47.698405
},
icon: {
url: 'https://image.flaticon.com/teams/slug/google.jpg',
scaledSize: this.google && new google.maps.Size(28, 28),
labelOrigin: this.google && new google.maps.Point(16,-10),
},
title: 'title',
label: {
text: 'label',
color: "black",
fontWeight: "bold",
fontSize: "12px"
}
},
{
position:
{
lat: 19.881369,
lng: 43.367863
},
icon: {
url: 'https://image.flaticon.com/teams/slug/google.jpg',
scaledSize: this.google && new google.maps.Size(28, 28),
labelOrigin: this.google && new google.maps.Point(16,-10),
},
title: 'title',
label: {
text: 'label',
color: "black",
fontWeight: "bold",
fontSize: "12px"
}
},
]
},
},
}
</script>
It most likely occurs since by the time when marker icon is getting initialized Google Maps API is not yet loaded which makes icon initialization to fail for the following properties:
icon: {
scaledSize: this.google && new google.maps.Size(28, 28),
labelOrigin: this.google && new google.maps.Point(16,-10),
}
You could consider the following options to circumvent this issue:
Option 1
Utilize $gmapApiPromiseLazy mixin from vue-google-maps to guarantee Google Maps API has been loaded, like this:
created() {
this.$gmapApiPromiseLazy().then(() => {
this.initialize(); //init once library has been loaded
});
},
Option 2
initialize icon properties via object literals instead of Google API specific objects like this:
icon: {
url: "https://image.flaticon.com/teams/slug/google.jpg",
scaledSize: {width: 28, height: 28},
labelOrigin: {x: 16, y: -10}
},