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:
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
add a comment |
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:
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
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
add a comment |
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:
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
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:
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
javascript reactjs react-chartjs
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
add a comment |
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
add a comment |
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>
)
}
}
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
add a comment |
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>
)
}
}
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
add a comment |
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>
)
}
}
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
add a comment |
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>
)
}
}
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>
)
}
}
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
add a comment |
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
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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