Chart.js bar chart not showing up - vue.js

this is my BarChart.vue file
<template>
<canvas :id="`myChart`"></canvas>
</template>
<script>
import Chart from 'chart.js/auto';
export default {
name: 'BarChart',
// props: ['id', 'labels'],
mounted() {
// const ctx = document.getElementById('myChart' + this.id).getContext('2d');
const ctx = document.getElementById('myChart' ).getContext('2d');
const myChart = new Chart(ctx, {
type: 'bar',
data: {
labels: this.labels,
datasets: [
{
label: '# of Votes',
data: [12, 19, 3, 5, 2, 3],
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(255, 206, 86, 0.2)',
'rgba(75, 192, 192, 0.2)',
'rgba(153, 102, 255, 0.2)',
'rgba(255, 159, 64, 0.2)',
],
borderColor: [
'rgba(255, 99, 132, 1)',
'rgba(54, 162, 235, 1)',
'rgba(255, 206, 86, 1)',
'rgba(75, 192, 192, 1)',
'rgba(153, 102, 255, 1)',
'rgba(255, 159, 64, 1)',
],
borderWidth: 5,
},
],
},
options: {
scales: {
y: {
beginAtZero: true,
},
},
},
});
myChart;
},
};
</script>
I am calling it as <BarChart /> in the parent file after adding it to the components, the chart doesn't show up but it creates a space in the DOM.
I have tried both vue-chart.js and normal chart.js, facing the same issue.

Related

How can I remove certain strokes in the react-native-chart-kit graph?

I want to remove all the strokes from this graph except the bottom one. I've tried going through the react-native-svg documentation and through the react-native-chart-kit documentation but I can't seem to find anything about it.
The graph:
<StackedBarChart
data={{
labels: ["S", "M", "T", "W", "Th", "F", "S"],
legend: ["L1", "L2"],
data: data,
barColors: ["#3d9e20", "#b21b1b", "#ffba00"]
}}
width={Dimensions.get("window").width * 0.8} // from react-native
height={220}
hideLegend={true}
withHorizontalLabels={true}
chartConfig={{
backgroundGradientFrom: "#422692",
backgroundGradientTo: "#422692",
color: (opacity = 1) => `rgba(255, 255, 255, ${opacity})`,
labelColor: (opacity = 1) => `rgba(255, 255, 255, ${opacity})`,
style: {
borderRadius: 16,
},
propsForBackgroundLines: {
strokeWidth: 5,
strokeDasharray: [],
},
propsForVerticalLabels: {
fontSize: 20,
dx: -7.4
},
}}
style={{
borderRadius: 16,
}}
/>
Set strokeWidth
propsForBackgroundLines: {
strokeDasharray: "",
strokeWidth: 0,
},

React native chart kit export to pdf

Is there a way to export charts to pdf in react native? Or i need to create them in html and use html-to-pdf library to create pdf?
Chart:
data={{
labels: ['Constipate', 'Normal', 'Diarrhea'],
datasets: [
{
data: [stoolBarChartData.constip, stoolBarChartData.normal, stoolBarChartData.diarrhea]
}
]
}}
fromZero
width={Dimensions.get("window").width - 40}
height={350}
showValuesOnTopOfBars={true}
withInnerLines={false}
withHorizontalLabels={false}
chartConfig={{
barPercentage: 1,
backgroundColor: "#0000",
backgroundGradientFrom: "white",
backgroundGradientTo: "white",
decimalPlaces: 0,
color: (opacity = 1) => `rgba(47, 80, 31, ${opacity})`,
labelColor: (opacity = 1) => `rgba(47, 80, 31, ${opacity})`,
style: {
borderRadius: 16
},
}}
style={{
marginVertical: 8,
borderRadius: 16,
margin: 20,
}}
/> ```
The only way I found is to render off-screen my charts and then take a snapshot of them with react-native-view-shot and then add them with react-native-html-to-pdf to pdf.

react-native-chart-kit Line chart with 1 segment doesn't print y-label

I have the below code where there are only 2 values on the y-axis which means a single segment should suffice. But it turns out the y-label for the top line doesnt come up. Is there a fix for this? Current Chart state
Below is the code I have written for this.
< LineChart
data = {
dataFrame
}
width = {
width - 20
}
height = {
height
}
withShadow = {
true
}
withDots = {
true
}
withScrollableDot = {
true
}
withOuterLines = {
true
}
transparent = {
true
}
withInnerLines = {
true
}
chartConfig = {
{
backgroundColor: '#fff000',
backgroundGradientFrom: '#ffffff',
backgroundGradientTo: '#fffffa',
decimalPlaces: 0,
linejoinType: 'round',
scrollableDotFill: '#fff',
scrollableDotRadius: 6,
scrollableDotStrokeColor: 'tomato',
scrollableDotStrokeWidth: 3,
scrollableInfoViewStyle: {
justifyContent: 'center',
alignContent: 'center',
backgroundColor: '#121212',
borderRadius: 2,
marginTop: 25,
marginLeft: 25,
},
scrollableInfoTextStyle: {
fontSize: 10,
color: '#C4C4C4',
marginHorizontal: 2,
flex: 1,
textAlign: 'center',
},
scrollableInfoSize: {
width: 30,
height: 30
},
scrollableInfoOffset: 15,
labelColor: (opacity = 1) => `rgba(0, 0, 0, ${opacity})`,
color: (opacity = 1) => `rgb(78, 135, 210, ${opacity})`,
propsForBackgroundLines: {
strokeDasharray: '', // solid background lines with no dashes
strokeDashoffset: 15,
},
}
}
segments = {1}
/>
Any help appreciated.
It looks to be a bug in the way the renderHorizontalLabels function is defined inside AbstractChart.tsx. If segments equals 1, only one horizontal label is actually returned instead of 2.
There is a workaround to achieve the effect you want. You could create a generator function and use the formatYLabel prop on the LineChart like this:
const d = [0, 110, 90, 130, 80, 103]; // example data
function* yLabel() {
const min = Math.min(...d); // minimum value of data array d
const max = Math.max(...d); // maximum value of data array d
yield* [min, '', max];
}
function App() {
// Instantiate the iterator
const yLabelIterator = yLabel();
return (
<LineChart
data={{
labels: ['January', 'February', 'March', 'April', 'May', 'June'],
datasets: [
{
data: d,
},
],
}}
width={400}
height={200}
withShadow={true}
withDots={true}
withScrollableDot={true}
withOuterLines={true}
transparent={true}
withInnerLines={true}
chartConfig={{
backgroundColor: '#fff000',
backgroundGradientFrom: '#ffffff',
backgroundGradientTo: '#fffffa',
decimalPlaces: 0,
linejoinType: 'round',
scrollableDotFill: '#fff',
scrollableDotRadius: 6,
scrollableDotStrokeColor: 'tomato',
scrollableDotStrokeWidth: 3,
scrollableInfoViewStyle: {
justifyContent: 'center',
alignContent: 'center',
backgroundColor: '#121212',
borderRadius: 2,
marginTop: 25,
marginLeft: 25,
},
scrollableInfoTextStyle: {
fontSize: 10,
color: '#C4C4C4',
marginHorizontal: 2,
flex: 1,
textAlign: 'center',
},
scrollableInfoSize: {
width: 30,
height: 30,
},
scrollableInfoOffset: 15,
labelColor: (opacity = 1) => `rgba(0, 0, 0, ${opacity})`,
color: (opacity = 1) => `rgb(78, 135, 210, ${opacity})`,
propsForBackgroundLines: {
strokeDasharray: '',
strokeDashoffset: 15,
},
}}
segments={2}
formatYLabel={() => yLabelIterator.next().value}
/>
);
}
So the idea here is to use a segments value of 2, but to show an empty string for the middle label which gives the effect you would expect to get when setting segments to 1.

Using react-native-svg-charts how to set x-axis and y-axis value in bar chart , whereas y-axis is related to time and x-axis is related to numbers?

DataSet Using = "Time": [
{
"Hr": 15,
"Number": 100,
"Min": 0,
},
{
"Hr": 22,
"Number": 70,
"Min": 0,
},
{
"Hr": 0,
"Number": 50,
"Min": 0,
},
{
"Hr": 5,
"Number": 70,
"Min": 0,
},
{
"Hr": 6,
"Number": 100,
"Min": 0,
}
]
const data1 = [
{
hr: '15:00',
},
{
hr: '19:45',
},
{
hr: '00:30',
},
{
hr: '05:15',
},
{
hr: '10:00',
},
]
Code
<View style={{ height: 150, flexDirection: 'row', flex: 1 }}>
<YAxis
style={{ paddingLeft: 15 }}
data={data}
yAccessor={({ item }) => item.number}
contentInset={{ top: 40, bottom: 40 }}
svg={{
fill: 'grey',
fontSize: 10,
}}
numberOfTicks={2}
scale={scale.scaleBand}
formatLabel={(_, index) => data[index].number}
/>
<BarChart style={{ width: 350, marginLeft: 5 }}
data=Time
svg={{ fill }}
contentInset={{ top: 30, bottom: 0 }}
numberOfTicks={3}
yAccessor={({ item }) => item.number}
>
<Grid />
</BarChart>
</View>
<XAxis
data={data1}
svg={{ fontSize: 10, fill: 'black' }}
scale={scale.scaleBand}
formatLabel={(_, index) => data1[index].label}
/>
</View>
Which looks exactly the way i dont want .In my bar chart x-axis is always static value ,Which consist of data1 values represents hr and y-axis consist of numbers starts always from 0 to 100, ex 0,50,100.In bar chart when i am passing hr =15 it should go till 100 and then end till 22 hr starts and goes on,please suggest if any one can help?

How can align items correctly using flexWrap in FlatList

https://imge.to/i/6seRw
I want it to look like instagram explore page. When i tried flexWrap:'wrap' there is an aligment error. Next item doesn't go the right place.
_renderItem({ item }) {
return (
<TouchableOpacity style={[{marginLeft:5,marginBottom:5},{width:item.width},{height:item.height}]} onPress={() => {console.log(item.key)}}>
<Image style={[{width:item.width},{height:item.height}]} key={item.key} source={{uri:item.image}} />
</TouchableOpacity>
);
}
render() {
const itemData = [
{key: 'A', width: 120, height: 120, image: 'https://via.placeholder.com/120.png'},
{key: 'B', width: 120, height: 120, image: 'https://via.placeholder.com/120.png'},
{key: 'C', width: 120, height: 120, image: 'https://via.placeholder.com/120.png'},
{key: 'D', width: 120, height: 120, image: 'https://via.placeholder.com/120.png'},
{key: 'E', width: 245, height: 245, image: 'https://via.placeholder.com/245.png'},
{key: 'F', width: 120, height: 120, image: 'https://via.placeholder.com/120.png'},
{key: 'G', width: 120, height: 120, image: 'https://via.placeholder.com/120.png'},
{key: 'H', width: 120, height: 120, image: 'https://via.placeholder.com/120.png'},
{key: 'I', width: 120, height: 120, image: 'https://via.placeholder.com/120.png'},
{key: 'J', width: 120, height: 120, image: 'https://via.placeholder.com/120.png'},
{key: 'K', width: 120, height: 120, image: 'https://via.placeholder.com/120.png'},
{key: 'L', width: 120, height: 120, image: 'https://via.placeholder.com/120.png'},
{key: 'M', width: 120, height: 120, image: 'https://via.placeholder.com/120.png'},
{key: 'N', width: 120, height: 120, image: 'https://via.placeholder.com/120.png'},
{key: 'O', width: 120, height: 120, image: 'https://via.placeholder.com/120.png'},
{key: 'P', width: 120, height: 120, image: 'https://via.placeholder.com/120.png'},
{key: 'R', width: 120, height: 120, image: 'https://via.placeholder.com/120.png'}
];
return(
<View style={styles.container}>
<FlatList
contentContainerStyle={{justifyContent: 'center',flexDirection:'row',flexWrap:'wrap'}}
data={itemData}
renderItem={this._renderItem}
/>
</View>
);
}
In this case use can use numColumns and columnWrapperStyle props from FlastList.
Here's example:
<FlatList
numColumns={2}
columnWrapperStyle={styles.columnWrapper}
keyExtractor={(val) => (`flat_list_item_${val.id}`)}
horizontal={false}
data={data}
renderItem={this.renderCard}
/>