Creating a graph with ReactJS and ChartJS











up vote
0
down vote

favorite












I am tying to create a graph using ChartJS and ReactJS. The issue that I am experiencing is that I cannot load my data array onto the graph.



There are no logged errors and this is how my chart looks currently:
enter image description here



I think that the mistake might be chartData object that I am using to pass the data:



  chartData:{
labels: this.timeDataArray,
datasets: this.priceDataArray,
backgroundColor:['rgba(255,99,132,0.6)']
}


I am using react-chartjs-2 as module for my charts and fetch for the http request.



Here is my full code:



export class Chart extends Component{
constructor(props){
super(props);
this.state = {
isLoading: false,
data: ,
chartData:{
labels: this.timeDataArray,
datasets: this.priceDataArray,
backgroundColor:['rgba(255,99,132,0.6)']
}
}
}

componentDidMount(){
this.fetchData();
}

fetchData(){
fetch("https://min-api.cryptocompare.com/data/histoday?fsym=BTC&tsym=USD&limit=600")
.then(response => response.json())
.then(data=>{
var fetchedData = data.Data;
var timeDataArray = ;
var priceDataArray = ;
console.log(timeDataArray)
console.log(priceDataArray)
for(var x = 0; x < fetchedData.length;x++){
var exampleTimeData = fetchedData[x].time
var examplePriceData = fetchedData[x].high
timeDataArray.push(exampleTimeData)
priceDataArray.push(examplePriceData)

}

})
.catch(error => console.log("parsing failed", error))
}

render(){
return(
<div className="chart">
<Line
data={this.state.chartData}
options={{
maintainAspectRatio:false
}}
/>
</div>
)
}
}


Question: Why is my data not being plotted since I am passing the label property with the time stamps and the actual data as the dataset ?










share|improve this question






















  • Are you having issues in general...like you cant get it to work even if you pass in some dummy data, or are you fine with that ?
    – born2gamble
    Nov 12 at 17:32










  • @born2gamble if look at the screenshot, you'll see that I cannot pass data or the data I'm passing is not in the correct format.
    – ZombieChowder
    Nov 12 at 17:35















up vote
0
down vote

favorite












I am tying to create a graph using ChartJS and ReactJS. The issue that I am experiencing is that I cannot load my data array onto the graph.



There are no logged errors and this is how my chart looks currently:
enter image description here



I think that the mistake might be chartData object that I am using to pass the data:



  chartData:{
labels: this.timeDataArray,
datasets: this.priceDataArray,
backgroundColor:['rgba(255,99,132,0.6)']
}


I am using react-chartjs-2 as module for my charts and fetch for the http request.



Here is my full code:



export class Chart extends Component{
constructor(props){
super(props);
this.state = {
isLoading: false,
data: ,
chartData:{
labels: this.timeDataArray,
datasets: this.priceDataArray,
backgroundColor:['rgba(255,99,132,0.6)']
}
}
}

componentDidMount(){
this.fetchData();
}

fetchData(){
fetch("https://min-api.cryptocompare.com/data/histoday?fsym=BTC&tsym=USD&limit=600")
.then(response => response.json())
.then(data=>{
var fetchedData = data.Data;
var timeDataArray = ;
var priceDataArray = ;
console.log(timeDataArray)
console.log(priceDataArray)
for(var x = 0; x < fetchedData.length;x++){
var exampleTimeData = fetchedData[x].time
var examplePriceData = fetchedData[x].high
timeDataArray.push(exampleTimeData)
priceDataArray.push(examplePriceData)

}

})
.catch(error => console.log("parsing failed", error))
}

render(){
return(
<div className="chart">
<Line
data={this.state.chartData}
options={{
maintainAspectRatio:false
}}
/>
</div>
)
}
}


Question: Why is my data not being plotted since I am passing the label property with the time stamps and the actual data as the dataset ?










share|improve this question






















  • Are you having issues in general...like you cant get it to work even if you pass in some dummy data, or are you fine with that ?
    – born2gamble
    Nov 12 at 17:32










  • @born2gamble if look at the screenshot, you'll see that I cannot pass data or the data I'm passing is not in the correct format.
    – ZombieChowder
    Nov 12 at 17:35













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I am tying to create a graph using ChartJS and ReactJS. The issue that I am experiencing is that I cannot load my data array onto the graph.



There are no logged errors and this is how my chart looks currently:
enter image description here



I think that the mistake might be chartData object that I am using to pass the data:



  chartData:{
labels: this.timeDataArray,
datasets: this.priceDataArray,
backgroundColor:['rgba(255,99,132,0.6)']
}


I am using react-chartjs-2 as module for my charts and fetch for the http request.



Here is my full code:



export class Chart extends Component{
constructor(props){
super(props);
this.state = {
isLoading: false,
data: ,
chartData:{
labels: this.timeDataArray,
datasets: this.priceDataArray,
backgroundColor:['rgba(255,99,132,0.6)']
}
}
}

componentDidMount(){
this.fetchData();
}

fetchData(){
fetch("https://min-api.cryptocompare.com/data/histoday?fsym=BTC&tsym=USD&limit=600")
.then(response => response.json())
.then(data=>{
var fetchedData = data.Data;
var timeDataArray = ;
var priceDataArray = ;
console.log(timeDataArray)
console.log(priceDataArray)
for(var x = 0; x < fetchedData.length;x++){
var exampleTimeData = fetchedData[x].time
var examplePriceData = fetchedData[x].high
timeDataArray.push(exampleTimeData)
priceDataArray.push(examplePriceData)

}

})
.catch(error => console.log("parsing failed", error))
}

render(){
return(
<div className="chart">
<Line
data={this.state.chartData}
options={{
maintainAspectRatio:false
}}
/>
</div>
)
}
}


Question: Why is my data not being plotted since I am passing the label property with the time stamps and the actual data as the dataset ?










share|improve this question













I am tying to create a graph using ChartJS and ReactJS. The issue that I am experiencing is that I cannot load my data array onto the graph.



There are no logged errors and this is how my chart looks currently:
enter image description here



I think that the mistake might be chartData object that I am using to pass the data:



  chartData:{
labels: this.timeDataArray,
datasets: this.priceDataArray,
backgroundColor:['rgba(255,99,132,0.6)']
}


I am using react-chartjs-2 as module for my charts and fetch for the http request.



Here is my full code:



export class Chart extends Component{
constructor(props){
super(props);
this.state = {
isLoading: false,
data: ,
chartData:{
labels: this.timeDataArray,
datasets: this.priceDataArray,
backgroundColor:['rgba(255,99,132,0.6)']
}
}
}

componentDidMount(){
this.fetchData();
}

fetchData(){
fetch("https://min-api.cryptocompare.com/data/histoday?fsym=BTC&tsym=USD&limit=600")
.then(response => response.json())
.then(data=>{
var fetchedData = data.Data;
var timeDataArray = ;
var priceDataArray = ;
console.log(timeDataArray)
console.log(priceDataArray)
for(var x = 0; x < fetchedData.length;x++){
var exampleTimeData = fetchedData[x].time
var examplePriceData = fetchedData[x].high
timeDataArray.push(exampleTimeData)
priceDataArray.push(examplePriceData)

}

})
.catch(error => console.log("parsing failed", error))
}

render(){
return(
<div className="chart">
<Line
data={this.state.chartData}
options={{
maintainAspectRatio:false
}}
/>
</div>
)
}
}


Question: Why is my data not being plotted since I am passing the label property with the time stamps and the actual data as the dataset ?







javascript reactjs react-chartjs






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 12 at 16:20









ZombieChowder

839527




839527












  • Are you having issues in general...like you cant get it to work even if you pass in some dummy data, or are you fine with that ?
    – born2gamble
    Nov 12 at 17:32










  • @born2gamble if look at the screenshot, you'll see that I cannot pass data or the data I'm passing is not in the correct format.
    – ZombieChowder
    Nov 12 at 17:35


















  • Are you having issues in general...like you cant get it to work even if you pass in some dummy data, or are you fine with that ?
    – born2gamble
    Nov 12 at 17:32










  • @born2gamble if look at the screenshot, you'll see that I cannot pass data or the data I'm passing is not in the correct format.
    – ZombieChowder
    Nov 12 at 17:35
















Are you having issues in general...like you cant get it to work even if you pass in some dummy data, or are you fine with that ?
– born2gamble
Nov 12 at 17:32




Are you having issues in general...like you cant get it to work even if you pass in some dummy data, or are you fine with that ?
– born2gamble
Nov 12 at 17:32












@born2gamble if look at the screenshot, you'll see that I cannot pass data or the data I'm passing is not in the correct format.
– ZombieChowder
Nov 12 at 17:35




@born2gamble if look at the screenshot, you'll see that I cannot pass data or the data I'm passing is not in the correct format.
– ZombieChowder
Nov 12 at 17:35












1 Answer
1






active

oldest

votes

















up vote
-1
down vote













I dont know if its my labtop right now or what, but after running it, I need to resize my window and the graph updates to show the data.



export default class Chart extends Component {
constructor(props) {
super(props)
this.state = {
isLoading: false,
chartData: {
labels: ['First', 'Second', 'Third', 'Fourth'],
datasets: [
{
label: ['My Chart'],
data: [100, 200, 300, 400],
backgroundColor: ['#26331d'],
borderWidth: 1,
borderColor: '#777',
hoverBorderWidth: 3,
hoverBorderColor: '#000'
}
]
}
}
}

componentDidMount() {
this.fetchData()
}

fetchData() {
var timeDataArray =
var priceDataArray =
fetch(
'https://min-api.cryptocompare.com/data/histoday?fsym=BTC&tsym=USD&limit=50'
)
.then(response => response.json())
.then(data => {
var fetchedData = data.Data

console.log(timeDataArray)
console.log(priceDataArray)

for (var x = 0; x < fetchedData.length; x++) {
var exampleTimeData = fetchedData[x].time
var examplePriceData = fetchedData[x].high
timeDataArray.push(exampleTimeData)
priceDataArray.push(examplePriceData)
}
})
.catch(error => console.log('parsing failed', error))
this.setState({
chartData: {
labels: timeDataArray,
datasets: [
{
label: ['Population'],
data: timeDataArray,
backgroundColor: ['#26331d', '#507935', '#63ef06', '#d0eabf'],
borderWidth: 1,
borderColor: '#777',
hoverBorderWidth: 3,
hoverBorderColor: '#000'
}
]
}
})
}

render() {
return (
<div className="chart">
<Line
data={this.state.chartData}
width={200}
height={200}
options={{
maintainAspectRatio: false
}}
/>
</div>
)
}
}





share|improve this answer























  • yes but I want to pass my data fetched from the given api, not from a static array.
    – ZombieChowder
    Nov 12 at 18:02










  • yup, was just making sure it was working with dummy data first, because I couldn't get that to work even with that with your base code
    – born2gamble
    Nov 12 at 18:15










  • Did that not work for you ?
    – born2gamble
    Nov 12 at 23:02











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53266179%2fcreating-a-graph-with-reactjs-and-chartjs%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
-1
down vote













I dont know if its my labtop right now or what, but after running it, I need to resize my window and the graph updates to show the data.



export default class Chart extends Component {
constructor(props) {
super(props)
this.state = {
isLoading: false,
chartData: {
labels: ['First', 'Second', 'Third', 'Fourth'],
datasets: [
{
label: ['My Chart'],
data: [100, 200, 300, 400],
backgroundColor: ['#26331d'],
borderWidth: 1,
borderColor: '#777',
hoverBorderWidth: 3,
hoverBorderColor: '#000'
}
]
}
}
}

componentDidMount() {
this.fetchData()
}

fetchData() {
var timeDataArray =
var priceDataArray =
fetch(
'https://min-api.cryptocompare.com/data/histoday?fsym=BTC&tsym=USD&limit=50'
)
.then(response => response.json())
.then(data => {
var fetchedData = data.Data

console.log(timeDataArray)
console.log(priceDataArray)

for (var x = 0; x < fetchedData.length; x++) {
var exampleTimeData = fetchedData[x].time
var examplePriceData = fetchedData[x].high
timeDataArray.push(exampleTimeData)
priceDataArray.push(examplePriceData)
}
})
.catch(error => console.log('parsing failed', error))
this.setState({
chartData: {
labels: timeDataArray,
datasets: [
{
label: ['Population'],
data: timeDataArray,
backgroundColor: ['#26331d', '#507935', '#63ef06', '#d0eabf'],
borderWidth: 1,
borderColor: '#777',
hoverBorderWidth: 3,
hoverBorderColor: '#000'
}
]
}
})
}

render() {
return (
<div className="chart">
<Line
data={this.state.chartData}
width={200}
height={200}
options={{
maintainAspectRatio: false
}}
/>
</div>
)
}
}





share|improve this answer























  • yes but I want to pass my data fetched from the given api, not from a static array.
    – ZombieChowder
    Nov 12 at 18:02










  • yup, was just making sure it was working with dummy data first, because I couldn't get that to work even with that with your base code
    – born2gamble
    Nov 12 at 18:15










  • Did that not work for you ?
    – born2gamble
    Nov 12 at 23:02















up vote
-1
down vote













I dont know if its my labtop right now or what, but after running it, I need to resize my window and the graph updates to show the data.



export default class Chart extends Component {
constructor(props) {
super(props)
this.state = {
isLoading: false,
chartData: {
labels: ['First', 'Second', 'Third', 'Fourth'],
datasets: [
{
label: ['My Chart'],
data: [100, 200, 300, 400],
backgroundColor: ['#26331d'],
borderWidth: 1,
borderColor: '#777',
hoverBorderWidth: 3,
hoverBorderColor: '#000'
}
]
}
}
}

componentDidMount() {
this.fetchData()
}

fetchData() {
var timeDataArray =
var priceDataArray =
fetch(
'https://min-api.cryptocompare.com/data/histoday?fsym=BTC&tsym=USD&limit=50'
)
.then(response => response.json())
.then(data => {
var fetchedData = data.Data

console.log(timeDataArray)
console.log(priceDataArray)

for (var x = 0; x < fetchedData.length; x++) {
var exampleTimeData = fetchedData[x].time
var examplePriceData = fetchedData[x].high
timeDataArray.push(exampleTimeData)
priceDataArray.push(examplePriceData)
}
})
.catch(error => console.log('parsing failed', error))
this.setState({
chartData: {
labels: timeDataArray,
datasets: [
{
label: ['Population'],
data: timeDataArray,
backgroundColor: ['#26331d', '#507935', '#63ef06', '#d0eabf'],
borderWidth: 1,
borderColor: '#777',
hoverBorderWidth: 3,
hoverBorderColor: '#000'
}
]
}
})
}

render() {
return (
<div className="chart">
<Line
data={this.state.chartData}
width={200}
height={200}
options={{
maintainAspectRatio: false
}}
/>
</div>
)
}
}





share|improve this answer























  • yes but I want to pass my data fetched from the given api, not from a static array.
    – ZombieChowder
    Nov 12 at 18:02










  • yup, was just making sure it was working with dummy data first, because I couldn't get that to work even with that with your base code
    – born2gamble
    Nov 12 at 18:15










  • Did that not work for you ?
    – born2gamble
    Nov 12 at 23:02













up vote
-1
down vote










up vote
-1
down vote









I dont know if its my labtop right now or what, but after running it, I need to resize my window and the graph updates to show the data.



export default class Chart extends Component {
constructor(props) {
super(props)
this.state = {
isLoading: false,
chartData: {
labels: ['First', 'Second', 'Third', 'Fourth'],
datasets: [
{
label: ['My Chart'],
data: [100, 200, 300, 400],
backgroundColor: ['#26331d'],
borderWidth: 1,
borderColor: '#777',
hoverBorderWidth: 3,
hoverBorderColor: '#000'
}
]
}
}
}

componentDidMount() {
this.fetchData()
}

fetchData() {
var timeDataArray =
var priceDataArray =
fetch(
'https://min-api.cryptocompare.com/data/histoday?fsym=BTC&tsym=USD&limit=50'
)
.then(response => response.json())
.then(data => {
var fetchedData = data.Data

console.log(timeDataArray)
console.log(priceDataArray)

for (var x = 0; x < fetchedData.length; x++) {
var exampleTimeData = fetchedData[x].time
var examplePriceData = fetchedData[x].high
timeDataArray.push(exampleTimeData)
priceDataArray.push(examplePriceData)
}
})
.catch(error => console.log('parsing failed', error))
this.setState({
chartData: {
labels: timeDataArray,
datasets: [
{
label: ['Population'],
data: timeDataArray,
backgroundColor: ['#26331d', '#507935', '#63ef06', '#d0eabf'],
borderWidth: 1,
borderColor: '#777',
hoverBorderWidth: 3,
hoverBorderColor: '#000'
}
]
}
})
}

render() {
return (
<div className="chart">
<Line
data={this.state.chartData}
width={200}
height={200}
options={{
maintainAspectRatio: false
}}
/>
</div>
)
}
}





share|improve this answer














I dont know if its my labtop right now or what, but after running it, I need to resize my window and the graph updates to show the data.



export default class Chart extends Component {
constructor(props) {
super(props)
this.state = {
isLoading: false,
chartData: {
labels: ['First', 'Second', 'Third', 'Fourth'],
datasets: [
{
label: ['My Chart'],
data: [100, 200, 300, 400],
backgroundColor: ['#26331d'],
borderWidth: 1,
borderColor: '#777',
hoverBorderWidth: 3,
hoverBorderColor: '#000'
}
]
}
}
}

componentDidMount() {
this.fetchData()
}

fetchData() {
var timeDataArray =
var priceDataArray =
fetch(
'https://min-api.cryptocompare.com/data/histoday?fsym=BTC&tsym=USD&limit=50'
)
.then(response => response.json())
.then(data => {
var fetchedData = data.Data

console.log(timeDataArray)
console.log(priceDataArray)

for (var x = 0; x < fetchedData.length; x++) {
var exampleTimeData = fetchedData[x].time
var examplePriceData = fetchedData[x].high
timeDataArray.push(exampleTimeData)
priceDataArray.push(examplePriceData)
}
})
.catch(error => console.log('parsing failed', error))
this.setState({
chartData: {
labels: timeDataArray,
datasets: [
{
label: ['Population'],
data: timeDataArray,
backgroundColor: ['#26331d', '#507935', '#63ef06', '#d0eabf'],
borderWidth: 1,
borderColor: '#777',
hoverBorderWidth: 3,
hoverBorderColor: '#000'
}
]
}
})
}

render() {
return (
<div className="chart">
<Line
data={this.state.chartData}
width={200}
height={200}
options={{
maintainAspectRatio: false
}}
/>
</div>
)
}
}






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 12 at 18:13

























answered Nov 12 at 17:48









born2gamble

609




609












  • yes but I want to pass my data fetched from the given api, not from a static array.
    – ZombieChowder
    Nov 12 at 18:02










  • yup, was just making sure it was working with dummy data first, because I couldn't get that to work even with that with your base code
    – born2gamble
    Nov 12 at 18:15










  • Did that not work for you ?
    – born2gamble
    Nov 12 at 23:02


















  • yes but I want to pass my data fetched from the given api, not from a static array.
    – ZombieChowder
    Nov 12 at 18:02










  • yup, was just making sure it was working with dummy data first, because I couldn't get that to work even with that with your base code
    – born2gamble
    Nov 12 at 18:15










  • Did that not work for you ?
    – born2gamble
    Nov 12 at 23:02
















yes but I want to pass my data fetched from the given api, not from a static array.
– ZombieChowder
Nov 12 at 18:02




yes but I want to pass my data fetched from the given api, not from a static array.
– ZombieChowder
Nov 12 at 18:02












yup, was just making sure it was working with dummy data first, because I couldn't get that to work even with that with your base code
– born2gamble
Nov 12 at 18:15




yup, was just making sure it was working with dummy data first, because I couldn't get that to work even with that with your base code
– born2gamble
Nov 12 at 18:15












Did that not work for you ?
– born2gamble
Nov 12 at 23:02




Did that not work for you ?
– born2gamble
Nov 12 at 23:02


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53266179%2fcreating-a-graph-with-reactjs-and-chartjs%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to change which sound is reproduced for terminal bell?

Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

Can I use Tabulator js library in my java Spring + Thymeleaf project?