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

Feat/piechart in multiplot #173

Open
wants to merge 46 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
e2280d1
Ajout class Pie
Tanguylo Jun 3, 2022
d83ec07
Start with piechart
Tanguylo Jun 7, 2022
d5b2e4b
feat(piechart): first piechart drawing
Tanguylo Jun 7, 2022
b4514ea
feat(piechart): add colors to piechart
Tanguylo Jun 8, 2022
e21e984
feat(piechart):add pie part surface coloring on mouse over
Tanguylo Jun 13, 2022
949df9c
feat(piechart):partial code cleaning
Tanguylo Jun 13, 2022
8427fef
feat(piechart):fix a git mistake with branches
Tanguylo Jun 13, 2022
e21259e
feat(piechart):fixing last diff in core.py
Tanguylo Jun 13, 2022
ad59106
feat(piechart):remove ' /' line 931
Tanguylo Jun 13, 2022
6c494e2
feat(piechart):add parts selection on click
Tanguylo Jun 14, 2022
9c83b56
feat(piechart):manage piepart paths as piepart attribute
Tanguylo Jun 14, 2022
38ebcb3
feat(piechart):initialize pieparts path when instantiating when crea…
Tanguylo Jun 14, 2022
1937904
feat(piechart):some code cleaning
Tanguylo Jun 17, 2022
58cca16
feat(piechart_in_multiplot): adds piechart case in multiplot
Tanguylo Jun 21, 2022
4675ede
feat(piechart): fix mistakes following the first 6 comments on this p…
Tanguylo Jun 21, 2022
cd1864f
feat(piechart): fix the 5 following remarks of the PR
Tanguylo Jun 21, 2022
1f81acc
feat(piechart): fix the remaining remarks except the drawPieChart method
Tanguylo Jun 21, 2022
e3371ec
feat(piechart): take last PR remarks into account
Tanguylo Jun 21, 2022
84fe419
feat(piechart): change type of color_to_plot_data to map
Tanguylo Jun 22, 2022
8bf219a
feat(piechart): modifications following PR remarks
Tanguylo Jun 22, 2022
006de63
feat(piechart): Changes following the PR review
Tanguylo Jun 24, 2022
3cea6f1
feat(piechart): Put color:string='' in the constructor of PieParts fo…
Tanguylo Jun 24, 2022
8a1f8da
feat(piechart_in_multiplot): Merge feat/piechart in feat/piechart_in_…
Tanguylo Jun 24, 2022
e1a8fa5
feat(piechart_in_multiplot): Handle selection of piePart as expected
Tanguylo Jun 24, 2022
174b386
feat(piechart_in_multiplot): Handle selection of piePart as expected
Tanguylo Jun 24, 2022
0413f2f
feat(piechart): add commit to branch and reset latest_selected_points…
Tanguylo Jun 27, 2022
6a8f1ea
feat(piechart_in_multiplot): add piechart in python command
Tanguylo Jun 27, 2022
87d5ef6
feat(piechart): Undo color_to_plot_data as a Map
Tanguylo Jun 27, 2022
10772ba
feat(piechart): Changes following second PR
Tanguylo Jun 27, 2022
34cdd69
feat(piechart): DataSample and DataSamples type definition and some c…
Tanguylo Jun 27, 2022
80b5b08
feat(piechart_in_multiplot): remove a console.log
Tanguylo Jun 27, 2022
64c9094
feat(piechart_in_multiplot): merge piechart in piechart_in_multiplot
Tanguylo Jun 27, 2022
7a5f0e6
feat(piechart): Changes in drawPieChart
Tanguylo Jun 28, 2022
54091b1
feat(piechart): Change multiple_plot.py file
Tanguylo Jun 28, 2022
d2a38ee
feat(piechart_in_multiplot): Draw a PieChart in a multiplot
Tanguylo Jun 28, 2022
23f045e
feat(piechart_in_multiplot): Change multiple_plot.py to diagnose the …
Tanguylo Jun 28, 2022
714c0f4
feat(piechart_in_multiplot): Merge origin/feat/piechart
Tanguylo Jun 28, 2022
feed8ec
feat(piechart_in_multiplot): Put some console.log in clean_view. Chec…
Tanguylo Jun 28, 2022
0ecc993
feat(piechart): Call definePieParts in PieChart constructor and not a…
Tanguylo Jun 28, 2022
e704341
Merge remote-tracking branch 'origin/feat/piechart' into feat/piechar…
Tanguylo Jun 28, 2022
b37595e
feat(piechart_in_multiplot): Center PieChart in multiplots
Tanguylo Jun 28, 2022
4dc6905
feat(piechart_in_multiplot): Center even the last piechart of a multi…
Tanguylo Jun 28, 2022
a5dfc2b
feat(piechart_in_multiplot): Remove a useless line
Tanguylo Jun 28, 2022
5c7c296
feat(piechart): Write PR equivalent in CHANGELOG.md file
Tanguylo Jun 28, 2022
fe39ee3
Merge remote-tracking branch 'origin/feat/piechart' into feat/piechar…
Tanguylo Jun 28, 2022
726c16c
feat(piechart_in_multiplot): Write PR equivalent in CHANGELOG.md file
Tanguylo Jun 28, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased => [0.7.2]
### Add
- Add Piechart drawing
- Add Piechart drawing in mutliplot

## [0.7.1]
### Add
Expand Down
26 changes: 26 additions & 0 deletions plot_data/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,30 @@ def __init__(self, x_variable: str, y_variable: str,
PlotDataObject.__init__(self, type_='scatterplot', name=name)


class PieChart(PlotDataObject):
"""
A class for drawing pie plots.

:param data_samples: [{'name_param_1': any,..., 'name_param_p': any},...,
{'name_param_1': any,..., 'name_param_p': any}]
:type data_samples: List[dict]
:param slicing_variable: variable that you want to use to fill pie parts
:type slicing_variable: str
"""

def __init__(self, slicing_variable: str,
data_samples: List[Any] = None,
name: str = ''):

self.slicing_variable = slicing_variable
if not data_samples:
self.data_samples = []
else:
self.data_samples = data_samples

PlotDataObject.__init__(self, type_='piechart', name=name)


class ScatterMatrix(PlotDataObject):
def __init__(self, elements: List[Any] = None, axes: List[str] = None,
point_style: PointStyle = None, surface_style: SurfaceStyle = None,
Expand Down Expand Up @@ -1151,6 +1175,8 @@ def plot_canvas(plot_data_object: Subclass[PlotDataObject],
template = templates.histogram_template
elif plot_type == "scattermatrix":
template = templates.scatter_matrix_template
elif plot_type == "piechart":
template = templates.piechart_template
else:
raise NotImplementedError('Type {} not implemented'.format(plot_type))

Expand Down
32 changes: 32 additions & 0 deletions plot_data/templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,3 +225,35 @@
</body>
</html>
''')


piechart_template = Template('''
<!DOCTYPE html>
<html lang="en">
<head>
<script src=$core_path></script>
</head>
<div id="app">
<canvas id="$canvas_id" width="2000" height="490"
style="border: 1px solid black;">
</canvas>

<!-- Sets the basepath for the library if not in same directory -->

<script>
var width = 0.95*window.innerWidth;
var height = Math.max(0.95*window.innerHeight, 350);

var data = $data;
var number_plot_data = data.length;

var plot_data = new PlotData.PlotPieChart(
data, width, height, true, 0, 0, $canvas_id.id
);
plot_data.define_canvas($canvas_id.id);
plot_data.draw_initial();
plot_data.mouse_interaction(false);
</script>
</div>
</html>
''')
45 changes: 36 additions & 9 deletions script/multiple_plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@
a list of vectors (dictionaries) that are displayed
through different representations such as parallel plots and scatter plots
"""
elements = []
data_samples = []

nb_elements = 50
nb_samples = 50
available_colors = [colors.VIOLET, colors.BLUE, colors.GREEN, colors.RED, colors.YELLOW, colors.CYAN, colors.ROSE]
directions = ['north', 'south', 'west', 'east']
for i in range(nb_elements):
for i in range(nb_samples):
random_color = available_colors[random.randint(0, len(available_colors) - 1)]
random_direction = directions[random.randint(0, len(directions) - 1)]
elements.append({'x': random.uniform(0, 200),
data_samples.append({'x': random.uniform(0, 200),
'y': random.uniform(0, 100),
'color': random_color,
'direction': random_direction})
Expand All @@ -35,12 +35,17 @@

"""Scatterplots"""
scatterplot1 = plot_data.Scatter(x_variable='x', y_variable='y')
piechart1 = plot_data.PieChart(data_samples=data_samples,
slicing_variable='mass')

scatterplot2 = plot_data.Scatter(x_variable='y', y_variable='color',
point_style=plot_data.PointStyle(shape='square')) # optional argument that changes
# points' appearance

scatterplot3 = plot_data.Scatter(x_variable='x', y_variable='direction')
# scatterplot3 = plot_data.Scatter(x_variable='x', y_variable='direction')
piechart1 = plot_data.PieChart(data_samples=data_samples,
slicing_variable='x')


"""PrimitiveGroupContainers"""
contour = plot_data.Contour2D(plot_data_primitives=[plot_data.LineSegment2D([1, 1], [1, 2]),
Expand All @@ -67,13 +72,35 @@

"""Creating the multiplot"""
plots = [parallelplot1, parallelplot2, scatterplot1,
scatterplot2, scatterplot3, graph2d, primitive_group_container,
scatterplot2, piechart1, graph2d, primitive_group_container,
histogram, graph2d, primitive_group_container,
histogram]

# plots = [scatterplot1, scatterplot2]

multiplot = plot_data.MultiplePlots(plots=plots, elements=elements,
multiplot = plot_data.MultiplePlots(plots=plots, elements=data_samples,
initial_view_on=True)

# Display
plot_data.plot_canvas(plot_data_object=multiplot, debug_mode=True)

plots2 = [piechart1, piechart1, piechart1, piechart1, piechart1, piechart1,
piechart1, piechart1, piechart1, piechart1, piechart1, piechart1,
piechart1, piechart1, piechart1, piechart1, piechart1]

multiplot2 = plot_data.MultiplePlots(plots=plots2, elements=data_samples,
initial_view_on=True)

# Display
plot_data.plot_canvas(plot_data_object=multiplot2, debug_mode=True)


plots3 = [scatterplot2, scatterplot2, scatterplot2, scatterplot2, scatterplot2, scatterplot2,
scatterplot2, scatterplot2, scatterplot2, scatterplot2]

multiplot3 = plot_data.MultiplePlots(plots=plots3, elements=data_samples,
initial_view_on=True)

# Display
plot_data.plot_canvas(plot_data_object=multiplot3, debug_mode=True)



35 changes: 35 additions & 0 deletions script/pie_chart.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Jun 3 16:18:31 2022

@author: tanguy
"""

import plot_data
import plot_data.colors as colors
import random


data_samples = []
SHAPES = ['round', 'square', 'triangle', 'ellipse']
COLORS = [colors.RED, colors.BLUE, colors.GREEN, colors.YELLOW, colors.ORANGE, colors.VIOLET]
for i in range(50):
random_shape = SHAPES[random.randint(0, len(SHAPES) - 1)]
random_color = COLORS[random.randint(0, len(SHAPES) - 1)]
data_samples.append({'mass': random.uniform(0, 50),
'length': random.uniform(0, 100),
'shape': random_shape,
'color': random_color
})


piechart1 = plot_data.PieChart(data_samples=data_samples,
slicing_variable='mass')

piechart2 = plot_data.PieChart(data_samples=data_samples,
slicing_variable='length')


plot_data.plot_canvas(plot_data_object=piechart1, debug_mode=True)
plot_data.plot_canvas(plot_data_object=piechart2, debug_mode=True)
27 changes: 19 additions & 8 deletions src/multiplots.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {PlotData, Interactions} from './plot-data';
import {Point2D} from './primitives';
import { Attribute, PointFamily, check_package_version, Window, TypeOf, equals, Sort, download } from './utils';
import { PlotContour, PlotScatter, ParallelPlot, PrimitiveGroupContainer, Histogram } from './subplots';
import { PlotContour, PlotScatter, ParallelPlot, PrimitiveGroupContainer, Histogram, PlotPieChart } from './subplots';
import { List, Shape, MyObject } from './toolbox';
import { string_to_hex, string_to_rgb, rgb_to_string } from './color_conversion';

Expand Down Expand Up @@ -85,6 +85,8 @@ export class MultiplePlots {
newObject = new ParallelPlot(this.dataObjects[i], this.sizes[i]['width'], this.sizes[i]['height'], buttons_ON, this.initial_coords[i][0], this.initial_coords[i][1], canvas_id, true);
} else if (object_type_ === 'primitivegroup') {
newObject = new PlotContour(this.dataObjects[i], this.sizes[i]['width'], this.sizes[i]['height'], buttons_ON, this.initial_coords[i][0], this.initial_coords[i][1], canvas_id, true);
} else if (object_type_ === 'piechart') {
newObject = new PlotPieChart(this.dataObjects[i], this.sizes[i]['width'], this.sizes[i]['height'], buttons_ON, this.initial_coords[i][0], this.initial_coords[i][1], canvas_id, true);
} else if (object_type_ === 'primitivegroupcontainer') {
newObject = new PrimitiveGroupContainer(this.dataObjects[i], this.sizes[i]['width'], this.sizes[i]['height'], buttons_ON, this.initial_coords[i][0], this.initial_coords[i][1], canvas_id, true);
if (this.dataObjects[i]['association']) {
Expand All @@ -109,6 +111,7 @@ export class MultiplePlots {
this.display_order.push(i);
this.to_display_plots.push(i);
}

this.mouse_interaction();

if (buttons_ON) {
Expand Down Expand Up @@ -1053,27 +1056,30 @@ export class MultiplePlots {
this.small_length_nb_objects = Math.min(Math.ceil(nbObjectsDisplayed/2), Math.floor(Math.sqrt(nbObjectsDisplayed)));
this.big_length_nb_objects = Math.ceil(nbObjectsDisplayed/this.small_length_nb_objects);
this.sorted_list = this.getSortedList();
// let big_length_step = this[big_length]/big_length_nb_objects;
// let small_length_step = this[small_length]/small_length_nb_objects;
// let big_length_step = this[big_length]/this.big_length_nb_objects;
// let small_length_step = this[small_length]/this.small_length_nb_objects;
let blank_space = this.padding || 0.01*this[small_length];
let big_length_step = (this[big_length] - (this.big_length_nb_objects + 1)*blank_space)/this.big_length_nb_objects;
let small_length_step = (this[small_length] - (this.small_length_nb_objects + 1)*blank_space)/this.small_length_nb_objects;

for (let i=0; i<this.big_length_nb_objects - 1; i++) {
for (let j=0; j<this.small_length_nb_objects; j++) {
var current_index = i*this.small_length_nb_objects + j; //current_index in sorted_list

// The three following lines are useful for primitive group containers only
let obj:any = this.objectList[this.sorted_list[current_index]];
let old_small_coord = obj[small_coord];
let old_big_coord = obj[big_coord];

this.objectList[this.sorted_list[current_index]][big_coord] = i*big_length_step + (i+1)*blank_space;
this.objectList[this.sorted_list[current_index]][small_coord] = j*small_length_step + (j+1)*blank_space;

this.objectList[this.sorted_list[current_index]][big_length] = big_length_step;
this.objectList[this.sorted_list[current_index]][small_length] = small_length_step;


if (this.objectList[this.sorted_list[current_index]] instanceof PlotPieChart){
this.objectList[this.sorted_list[current_index]]["origin" + big_coord] = big_length_step / 2;
this.objectList[this.sorted_list[current_index]]["origin" + small_coord] = small_length_step / 2;
}

if (obj.type_ === 'primitivegroupcontainer') {
for (let k=0; k<obj.primitive_groups.length; k++) {
obj.primitive_groups[k][big_coord] += obj[big_coord] - old_big_coord;
Expand All @@ -1100,6 +1106,11 @@ export class MultiplePlots {
this.objectList[this.sorted_list[last_index + j]][big_length] = big_length_step;
this.objectList[this.sorted_list[last_index + j]][small_length] = last_small_length_step;

if (this.objectList[this.sorted_list[last_index + j]] instanceof PlotPieChart){
this.objectList[this.sorted_list[last_index + j]]["origin" + big_coord] = big_length_step / 2;
this.objectList[this.sorted_list[last_index + j]]["origin" + small_coord] = last_small_length_step / 2;
}

if (obj.type_ === 'primitivegroupcontainer') {
for (let k=0; k<obj.primitive_groups.length; k++) {
obj.primitive_groups[k][big_coord] += obj[big_coord] - old_big_coord;
Expand Down
Loading