Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

3D Scatter Plot #484

Closed
yosiasz opened this issue Aug 3, 2023 · 2 comments · Fixed by #486
Closed

3D Scatter Plot #484

yosiasz opened this issue Aug 3, 2023 · 2 comments · Fixed by #486
Assignees
Labels
question Further information is requested
Milestone

Comments

@yosiasz
Copy link

yosiasz commented Aug 3, 2023

create a dashboard

point to Dynamic Text

no need for data source as we will use dynamic fake data

Enable Javascript for Dynamic Text here
image

add the following Script as external resource
https://cdn.plot.ly/plotly-2.24.1.min.js

add following html

<body>
  <div id="addisAbeba">
    <!-- Plotly chart will be drawn inside this DIV -->
  </div>
</body>

add following javascript


(async () => {
  let url = 'https://raw.githubusercontent.com/plotly/datasets/master/3d-scatter.csv';

  let fetchData = await fetch(url);
  let csvData = await fetchData.text();
  let rows = csvToJson(csvData);

  var trace1 = {
    x: unpack(rows, 'x1'), y: unpack(rows, 'y1'), z: unpack(rows, 'z1'),
    mode: 'markers',
    marker: {
      size: 12,
      line: {
        color: 'rgba(217, 217, 217, 0.14)',
        width: 0.5
      },
      opacity: 0.8
    },
    type: 'scatter3d'
  };

  var trace2 = {
    x: unpack(rows, 'x2'), y: unpack(rows, 'y2'), z: unpack(rows, 'z2'),
    mode: 'markers',
    marker: {
      color: 'rgb(127, 127, 127)',
      size: 12,
      symbol: 'circle',
      line: {
        color: 'rgb(204, 204, 204)',
        width: 1
      },
      opacity: 0.8
    },
    type: 'scatter3d'
  };

  var data = [trace1, trace2];

  var layout = {
    margin: {
      l: 0,
      r: 0,
      b: 0,
      t: 0
    }
  };
  Plotly.newPlot('addisAbeba', data, layout);
})();

const unpack = (rows, key) => {
  return rows.map(function (row) { return row[key]; });
}

const csvToJson = (data, delimiter = ',') => {
  const titles = data.slice(0, data.indexOf('\r\n')).split(delimiter);
  return data
    .slice(data.indexOf('\n') + 1)
    .split('\r\n')
    .map(v => {
      const values = v.split(delimiter);
      return titles.reduce(
        (obj, title, index) => ((obj[title] = values[index]), obj),
        {}
      );
    });
};

Results in

image

@yosiasz
Copy link
Author

yosiasz commented Aug 3, 2023

Plotly 3D Scatter

@mikhail-vl mikhail-vl self-assigned this Aug 3, 2023
@mikhail-vl mikhail-vl added the question Further information is requested label Aug 3, 2023
@mikhail-vl mikhail-vl added this to the Version 4.1.0 milestone Aug 3, 2023
@mikhail-vl mikhail-vl changed the title 3D SCatter Plot 3D Scatter Plot Aug 9, 2023
@mikhail-vl
Copy link
Member

Thank you @yosiasz, I updated the previous solution with 3d Scatter, which load external data and is more interesting.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants