Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

343
Visualizações
Updating component realtime based on Vue + apexchart + websocket

I have a chart like so:

.....
      <div class="flex flex-auto">
        <apexchart
          width="300%"
          height="380"
          type="line"
          :options="optionsLine"
          :series="seriesLine"
        ></apexchart>
.....

And the vue component has a method like so:

    methods: {
      getTraceData: function (data) {
       var esocket = new WebSocket(
        "ws://localhost:5001/provideTraceData?serverHost=" + data
      );
      esocket.onmessage = function (event) {
        var res = JSON.parse(event.data);
        var ts = [];
        var dataLength = res["timeseries"].length;

        console.log(res);
        for (var i = dataLength - 1; i > dataLength - 11; i--) {
          ts.push([
            res["timeseries"][i]["timestamp"],
            res["timeseries"][i]["ttfb"],
          ]);
        }
        console.log("==", ts);

        this.optionsLine = {
          tooltip: {
            shared: false,
            x: {
              formatter: function (val) {
                var date = new Date(val * 1000);
                // Hours part from the timestamp
                var hours = date.getHours();
                // Minutes part from the timestamp
                var minutes = "0" + date.getMinutes();
                // Seconds part from the timestamp
                var seconds = "0" + date.getSeconds();

                // Will display time in 10:30:23 format
                var formattedTime =
                  hours + ":" + minutes.substr(-2) + ":" + seconds.substr(-2);
                return formattedTime;
              },
            },
          },
        };

        this.seriesLine = [
          {
            name: "TTFB",
            data: ts,
          },
        ];
      };
   }
}

From the WebSocket server I get a JSON response, I parse it, etc, etc, and then try to update the chart series. But for some reason, the chart does not seem to render. But when I use a basic HTTP endpoint without WebSocket to get the same data then the chart renders. I'm sort of new javascript, is there some behind the scene async shenanigans happening I'm unaware of? The logic of updating the chart is same, the difference between the HTTP and WebSocket versions is basically the URL string and the event handler, the rest is the same.

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda