React Victory Line Chart For 0-10 minute timeframe










2















Could use some help trying to create a simple line chart with Victory.



What I'm trying to do:



I'm basically trying to create a line chart that shows random numbers for the last 10 minutes. I generate a new random number every 3 seconds, and add that num to the line chart.



So the X-Axis should be from 0 minutes – 10 minutes, and the Y axis should be the actual rand num for a given time.



My main problem is that I am pretty lost on how to go about creating the X axis from 0 – 10 minutes in 3 second intervals



What I have so far:



Here's a Code Sandbox with what I've done so far so you can try it out: https://codesandbox.io/s/6wnzkz512n



The main Chart component:



import React from 'react'
import VictoryChart, VictoryLine, VictoryAxis from 'victory'

class Chart extends React.Component
constructor()
super()
this.state =
data:



// Add a new data point every 3 seconds
componentDidMount()
this.getRandNum()
setInterval(this.getRandNum, 3000)


// get rand num from 1-5 along with current time,
// and add it to data. not sure if this is right approach
getRandNum = () =>
const newData =
date: new Date(),
num: Math.floor(Math.random() * 5) + 1


this.setState(
data: [...this.state.data, newData]
)


render()
return (
<VictoryChart width=600 height=470>
<VictoryLine
style=
data: stroke: 'lime'

data=this.state.data
x="date"
y="num"
/>
</VictoryChart>
)













share|improve this question




























    2















    Could use some help trying to create a simple line chart with Victory.



    What I'm trying to do:



    I'm basically trying to create a line chart that shows random numbers for the last 10 minutes. I generate a new random number every 3 seconds, and add that num to the line chart.



    So the X-Axis should be from 0 minutes – 10 minutes, and the Y axis should be the actual rand num for a given time.



    My main problem is that I am pretty lost on how to go about creating the X axis from 0 – 10 minutes in 3 second intervals



    What I have so far:



    Here's a Code Sandbox with what I've done so far so you can try it out: https://codesandbox.io/s/6wnzkz512n



    The main Chart component:



    import React from 'react'
    import VictoryChart, VictoryLine, VictoryAxis from 'victory'

    class Chart extends React.Component
    constructor()
    super()
    this.state =
    data:



    // Add a new data point every 3 seconds
    componentDidMount()
    this.getRandNum()
    setInterval(this.getRandNum, 3000)


    // get rand num from 1-5 along with current time,
    // and add it to data. not sure if this is right approach
    getRandNum = () =>
    const newData =
    date: new Date(),
    num: Math.floor(Math.random() * 5) + 1


    this.setState(
    data: [...this.state.data, newData]
    )


    render()
    return (
    <VictoryChart width=600 height=470>
    <VictoryLine
    style=
    data: stroke: 'lime'

    data=this.state.data
    x="date"
    y="num"
    />
    </VictoryChart>
    )













    share|improve this question


























      2












      2








      2








      Could use some help trying to create a simple line chart with Victory.



      What I'm trying to do:



      I'm basically trying to create a line chart that shows random numbers for the last 10 minutes. I generate a new random number every 3 seconds, and add that num to the line chart.



      So the X-Axis should be from 0 minutes – 10 minutes, and the Y axis should be the actual rand num for a given time.



      My main problem is that I am pretty lost on how to go about creating the X axis from 0 – 10 minutes in 3 second intervals



      What I have so far:



      Here's a Code Sandbox with what I've done so far so you can try it out: https://codesandbox.io/s/6wnzkz512n



      The main Chart component:



      import React from 'react'
      import VictoryChart, VictoryLine, VictoryAxis from 'victory'

      class Chart extends React.Component
      constructor()
      super()
      this.state =
      data:



      // Add a new data point every 3 seconds
      componentDidMount()
      this.getRandNum()
      setInterval(this.getRandNum, 3000)


      // get rand num from 1-5 along with current time,
      // and add it to data. not sure if this is right approach
      getRandNum = () =>
      const newData =
      date: new Date(),
      num: Math.floor(Math.random() * 5) + 1


      this.setState(
      data: [...this.state.data, newData]
      )


      render()
      return (
      <VictoryChart width=600 height=470>
      <VictoryLine
      style=
      data: stroke: 'lime'

      data=this.state.data
      x="date"
      y="num"
      />
      </VictoryChart>
      )













      share|improve this question
















      Could use some help trying to create a simple line chart with Victory.



      What I'm trying to do:



      I'm basically trying to create a line chart that shows random numbers for the last 10 minutes. I generate a new random number every 3 seconds, and add that num to the line chart.



      So the X-Axis should be from 0 minutes – 10 minutes, and the Y axis should be the actual rand num for a given time.



      My main problem is that I am pretty lost on how to go about creating the X axis from 0 – 10 minutes in 3 second intervals



      What I have so far:



      Here's a Code Sandbox with what I've done so far so you can try it out: https://codesandbox.io/s/6wnzkz512n



      The main Chart component:



      import React from 'react'
      import VictoryChart, VictoryLine, VictoryAxis from 'victory'

      class Chart extends React.Component
      constructor()
      super()
      this.state =
      data:



      // Add a new data point every 3 seconds
      componentDidMount()
      this.getRandNum()
      setInterval(this.getRandNum, 3000)


      // get rand num from 1-5 along with current time,
      // and add it to data. not sure if this is right approach
      getRandNum = () =>
      const newData =
      date: new Date(),
      num: Math.floor(Math.random() * 5) + 1


      this.setState(
      data: [...this.state.data, newData]
      )


      render()
      return (
      <VictoryChart width=600 height=470>
      <VictoryLine
      style=
      data: stroke: 'lime'

      data=this.state.data
      x="date"
      y="num"
      />
      </VictoryChart>
      )










      reactjs charts linechart victory-charts






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 11 '18 at 12:27









      Zoe

      11.6k74379




      11.6k74379










      asked Nov 11 '18 at 12:21









      inhwrbpinhwrbp

      1206




      1206






















          1 Answer
          1






          active

          oldest

          votes


















          0














          Code Sandbox with solution: https://codesandbox.io/s/989zx8v6v4



          Changes:



          • Add startTime in state


          • state.data.date rename as state.data.time and contains number of seconds from stat

          • Initialization in componentDidMount()


          • getRandNum() calculate time difference in seconds


          • VictoryAxis format X axis as "m:ss"

          Chart component:



           class Chart extends React.Component 
          constructor()
          super();
          this.state =
          data: ,
          startTime: null
          ;


          // Add a new data point every 3 seconds
          componentDidMount()
          const startTime = new Date();
          const time = 0;
          const num = Math.floor(Math.random() * 5) + 1;

          this.setState( data: [ time, num ], startTime );
          setInterval(this.getRandNum, 3000);


          // get rand num from 1-5 along with current time,
          // and add it to data. not sure if this is right approach
          getRandNum = () =>
          const actualTime = new Date();
          let num = Math.floor(Math.random() * 5) + 1;
          let time = Math.round((actualTime - this.state.startTime) / 1000);

          this.setState(
          data: [...this.state.data, time, num ]
          );
          ;

          render()
          return (
          <VictoryChart width=600 height=470>
          <VictoryAxis dependentAxis />
          <VictoryAxis
          tickFormat=t =>
          `$Math.floor(t / 60):$Math.round(t % 60)
          .toString()
          .padStart(2, "0")`

          />
          <VictoryLine
          style=
          data: stroke: "lime"

          data=this.state.data
          x="time"
          y="num"
          />
          </VictoryChart>
          );







          share|improve this answer






















            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',
            autoActivateHeartbeat: false,
            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%2f53248708%2freact-victory-line-chart-for-0-10-minute-timeframe%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









            0














            Code Sandbox with solution: https://codesandbox.io/s/989zx8v6v4



            Changes:



            • Add startTime in state


            • state.data.date rename as state.data.time and contains number of seconds from stat

            • Initialization in componentDidMount()


            • getRandNum() calculate time difference in seconds


            • VictoryAxis format X axis as "m:ss"

            Chart component:



             class Chart extends React.Component 
            constructor()
            super();
            this.state =
            data: ,
            startTime: null
            ;


            // Add a new data point every 3 seconds
            componentDidMount()
            const startTime = new Date();
            const time = 0;
            const num = Math.floor(Math.random() * 5) + 1;

            this.setState( data: [ time, num ], startTime );
            setInterval(this.getRandNum, 3000);


            // get rand num from 1-5 along with current time,
            // and add it to data. not sure if this is right approach
            getRandNum = () =>
            const actualTime = new Date();
            let num = Math.floor(Math.random() * 5) + 1;
            let time = Math.round((actualTime - this.state.startTime) / 1000);

            this.setState(
            data: [...this.state.data, time, num ]
            );
            ;

            render()
            return (
            <VictoryChart width=600 height=470>
            <VictoryAxis dependentAxis />
            <VictoryAxis
            tickFormat=t =>
            `$Math.floor(t / 60):$Math.round(t % 60)
            .toString()
            .padStart(2, "0")`

            />
            <VictoryLine
            style=
            data: stroke: "lime"

            data=this.state.data
            x="time"
            y="num"
            />
            </VictoryChart>
            );







            share|improve this answer



























              0














              Code Sandbox with solution: https://codesandbox.io/s/989zx8v6v4



              Changes:



              • Add startTime in state


              • state.data.date rename as state.data.time and contains number of seconds from stat

              • Initialization in componentDidMount()


              • getRandNum() calculate time difference in seconds


              • VictoryAxis format X axis as "m:ss"

              Chart component:



               class Chart extends React.Component 
              constructor()
              super();
              this.state =
              data: ,
              startTime: null
              ;


              // Add a new data point every 3 seconds
              componentDidMount()
              const startTime = new Date();
              const time = 0;
              const num = Math.floor(Math.random() * 5) + 1;

              this.setState( data: [ time, num ], startTime );
              setInterval(this.getRandNum, 3000);


              // get rand num from 1-5 along with current time,
              // and add it to data. not sure if this is right approach
              getRandNum = () =>
              const actualTime = new Date();
              let num = Math.floor(Math.random() * 5) + 1;
              let time = Math.round((actualTime - this.state.startTime) / 1000);

              this.setState(
              data: [...this.state.data, time, num ]
              );
              ;

              render()
              return (
              <VictoryChart width=600 height=470>
              <VictoryAxis dependentAxis />
              <VictoryAxis
              tickFormat=t =>
              `$Math.floor(t / 60):$Math.round(t % 60)
              .toString()
              .padStart(2, "0")`

              />
              <VictoryLine
              style=
              data: stroke: "lime"

              data=this.state.data
              x="time"
              y="num"
              />
              </VictoryChart>
              );







              share|improve this answer

























                0












                0








                0







                Code Sandbox with solution: https://codesandbox.io/s/989zx8v6v4



                Changes:



                • Add startTime in state


                • state.data.date rename as state.data.time and contains number of seconds from stat

                • Initialization in componentDidMount()


                • getRandNum() calculate time difference in seconds


                • VictoryAxis format X axis as "m:ss"

                Chart component:



                 class Chart extends React.Component 
                constructor()
                super();
                this.state =
                data: ,
                startTime: null
                ;


                // Add a new data point every 3 seconds
                componentDidMount()
                const startTime = new Date();
                const time = 0;
                const num = Math.floor(Math.random() * 5) + 1;

                this.setState( data: [ time, num ], startTime );
                setInterval(this.getRandNum, 3000);


                // get rand num from 1-5 along with current time,
                // and add it to data. not sure if this is right approach
                getRandNum = () =>
                const actualTime = new Date();
                let num = Math.floor(Math.random() * 5) + 1;
                let time = Math.round((actualTime - this.state.startTime) / 1000);

                this.setState(
                data: [...this.state.data, time, num ]
                );
                ;

                render()
                return (
                <VictoryChart width=600 height=470>
                <VictoryAxis dependentAxis />
                <VictoryAxis
                tickFormat=t =>
                `$Math.floor(t / 60):$Math.round(t % 60)
                .toString()
                .padStart(2, "0")`

                />
                <VictoryLine
                style=
                data: stroke: "lime"

                data=this.state.data
                x="time"
                y="num"
                />
                </VictoryChart>
                );







                share|improve this answer













                Code Sandbox with solution: https://codesandbox.io/s/989zx8v6v4



                Changes:



                • Add startTime in state


                • state.data.date rename as state.data.time and contains number of seconds from stat

                • Initialization in componentDidMount()


                • getRandNum() calculate time difference in seconds


                • VictoryAxis format X axis as "m:ss"

                Chart component:



                 class Chart extends React.Component 
                constructor()
                super();
                this.state =
                data: ,
                startTime: null
                ;


                // Add a new data point every 3 seconds
                componentDidMount()
                const startTime = new Date();
                const time = 0;
                const num = Math.floor(Math.random() * 5) + 1;

                this.setState( data: [ time, num ], startTime );
                setInterval(this.getRandNum, 3000);


                // get rand num from 1-5 along with current time,
                // and add it to data. not sure if this is right approach
                getRandNum = () =>
                const actualTime = new Date();
                let num = Math.floor(Math.random() * 5) + 1;
                let time = Math.round((actualTime - this.state.startTime) / 1000);

                this.setState(
                data: [...this.state.data, time, num ]
                );
                ;

                render()
                return (
                <VictoryChart width=600 height=470>
                <VictoryAxis dependentAxis />
                <VictoryAxis
                tickFormat=t =>
                `$Math.floor(t / 60):$Math.round(t % 60)
                .toString()
                .padStart(2, "0")`

                />
                <VictoryLine
                style=
                data: stroke: "lime"

                data=this.state.data
                x="time"
                y="num"
                />
                </VictoryChart>
                );








                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 12 '18 at 11:20









                Boris TraljićBoris Traljić

                634311




                634311



























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53248708%2freact-victory-line-chart-for-0-10-minute-timeframe%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

                    𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

                    Edmonton

                    Crossroads (UK TV series)