1

Hi I have this reacr/flask app that when you put a keyword it sends it to flask check some data and then sents it back to react as the following json response:

enter image description here

I want to be able to plot these values using 'highcharts-react-official' package.

As of now im able to see my response on the console but nothing on the chart. I changed the dates into that timestamps but not sure if it's better or I should be using datetime, I can change it back if necessary.

My code is the following:

import React, { Component } from 'react';
import axios from 'axios';
import Highcharts from 'highcharts'
import HighchartsReact from 'highcharts-react-official'


class Keyword extends Component {

  state = {
    kws :[]
  }


keyword = e => {
    e.preventDefault();
    axios.post("/trends",{search_keyword: document.getElementById("keywords").value})
    .then((res) => {
    

      const data = res.data

      const keyword = data.data
  
      console.log("first index", keyword[1])


       this.setState({kws: keyword})
      

    }
    )}



  render() {

    const {kws =[]} = this.state

    const options = {
  title: {
    text: 'My chart'
  },
  xAxis: {
    type: 'datetime'
  },
  series: [{
    data: kws
  }]
}

    return (


<HighchartsReact
    highcharts={Highcharts}
    options={options}
  />
);
  }
}

Im looking to plot something like this:

enter image description here

Update: I Have updated the JSON response to be like this:

enter image description here

Would this be a better structure for plotting? If so what do I do to plot each keyword and its values like the graph above?

UPDATE2: I have updated my Json file enter image description here

6
  • I would say the data for the series is not the correct format that the chart can understand... at a quick glance you are giving the data an array of objects. Your data has timestapms as keys as well, you might want to manipulate that. Have you looked through the HighCharts docs? github.com/highcharts/highcharts-react#optimal-way-to-update Commented Jan 23, 2021 at 23:51
  • What would be the correct way? I can transpose the JSON response so it's the other way. Would that be better? Commented Jan 23, 2021 at 23:54
  • Well you have price action over time... and need to give the series as an array of numerals series: [ { data: [1, 2, 3] } ], Commented Jan 23, 2021 at 23:56
  • This gives an excellent example of how the data looks for the series... and yours does not match this... stackblitz.com/edit/react-4ded5d?file=index.js Commented Jan 24, 2021 at 0:01
  • I see. So having the data like this would work? imgur.com/a/3dP09Du?? Commented Jan 24, 2021 at 2:22

1 Answer 1

2

You need to parse your data into the proper format which Highcharts will be able to recognize. Base on your screen from an update it should be something like below:

series: [{
  name: 'ps2',
  data: [
    [1579996800, 67],
    [1580601600, 70],
    [1581206400, 68],
    [1581811200, 72],
    etc...
  ]
}, {
  name: 'ps2 emulator',
  data: [
    [1579996800, 59],
    [1580601600, 69],
    [1581206400, 71],
    [1581811200, 63],
    etc...
  ]
}]
Sign up to request clarification or add additional context in comments.

7 Comments

I see, So I'm building the JSON response on python. To do this should I make the keyword( ps2, ps2 emulator) the index of the DF? Also when transforming the df into json im using to_json(orient="table") should I use jsonify? Maybe I should ask a different question :D But this is super helpful and sends me the right direction
@Sundios sorry, I don't know python. I just showed how the series object should look like. More: highcharts.com/docs/chart-concepts/series
Hi Sebastian, thanks for your help. I have updated the JSON response. Would that be a correct response for high chart to plot the results? or DateTime has to be like you showed like DateTime, value instead of "datetime": value?
@Sundios could you do a copy/paste of this JSON response to some online editor that I could test?
Thank you for your help Sebastian. Here is the example. jsfiddle.net/qLzmoup3
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.