diff --git a/mysite/plots/polygon.py b/mysite/plots/polygon.py index b959ea7dd5ef6da910d8d30a2258eb0b5fc44956..db535654921a09dd1ba3b1557523e50dd60dc66e 100644 --- a/mysite/plots/polygon.py +++ b/mysite/plots/polygon.py @@ -1,6 +1,7 @@ import sys, path +import mpld3 #import import_ipynb -import plots.avl_tree as avl_tree +import plots.avl_tree from matplotlib import pyplot as plt from shapely.geometry import Point, Polygon, LineString from shapely.geometry.base import BaseGeometry, geos_geom_from_py @@ -8,11 +9,16 @@ from shapely import affinity import numpy import numpy as np import math -import mpld3 import mplcursors from fractions import Fraction import random from copy import deepcopy +from plotly.subplots import make_subplots +import plotly.graph_objects as go +from plotly.offline import plot +from bokeh.plotting import figure +from bokeh.io import output_notebook, show +from bokeh.layouts import gridplot a = 0.407 @@ -88,23 +94,31 @@ class Polygon2(object): 0]) # m = y2-y1/x2-x1 Formel für die Geradensteigung mithilfe aus zwei verschiedenen Punkten der Geraden return slope - def plot_polygon(self, title=None): - fig, ax = plt.subplots(facecolor='w', edgecolor='k') - x = self.x_values - y = self.y_values - for x_text, y_text in self.shell: - ax.text(x_text, y_text, '({}, {})'.format(int(x_text * 10) / 10, int(y_text * 10) / 10)) - ax.plot(x, y) - ax.scatter(x, y, s=60) - if title == None: - title = 'A single plot' - ax.set_title(title) - x1 = [x[0] for x in self.spine] - y1 = [x[1] for x in self.spine] - ax.plot(x1, y1) - plt.close() - #mplcursors.cursor() - return mpld3.fig_to_html(fig) + def plot_polygon(self, render=True): + """Plotting a Polygon with bokeh""" + height = (int(self.height * 10)) / 10 + if self.slope == math.inf: + slope = math.inf + else: + slope = (int(self.slope * 10)) / 10 + x_data = self.x_values + y_data = self.y_values + TOOLTIPS = [("index", "$index"), ("(x,y)", "($x, $y)"), ] + title = 'height:{} slope:{}'.format(height, slope) + fig = figure(title=title, x_axis_label='x', y_axis_label='y', tooltips=TOOLTIPS, ) + fig.line(x_data, y_data, legend_label="Shell.", line_width=2, muted_alpha=0.2) + spine_x_values = [x[0] for x in self.spine] + spine_y_values = [x[1] for x in self.spine] + fig.line(spine_x_values, spine_y_values, line_color="red", legend_label="Spine", line_width=2, muted_alpha=0.2) + fig.legend.location = "top_left" + fig.legend.click_policy = "mute" + if render: + return show(fig) + else: + return fig + + + def splitter(self): left = [] @@ -183,6 +197,52 @@ def height_classes(polygon_list): return height_classes +def high_classes_plot(high_classes): + plot_high_class_list = [] + polygon_number = 0 + polygon_title_number = 0 + for hc in high_classes: + polygon_count = len(hc.polygons) + cols = 4 + rows = math.ceil(polygon_count / cols) + + # sub_plot_titles = ['{} height:{} slope:{}'.format(number, + # (int(hc.spine_ordered_polygons[number].height * 10)) / 10, + # (int(hc.spine_ordered_polygons[number].slope * 10)) / 10) + # for number in range(0, polygon_count)] + sub_plot_titles = [] + for polygon in hc.spine_ordered_polygons: + sub_plot_titles.append( + '{} height:{} slope:{}'.format(polygon_title_number, (int(polygon.height * 10)) / 10, + (int(polygon.slope * 10)) / 10)) + polygon_title_number += 1 + fig = make_subplots(rows=rows, cols=cols, subplot_titles=sub_plot_titles) + fig.update_layout(title="Highclass {} heights: {}-{}".format(hc.i, int(hc.min_border), int(hc.max_border))) + counter = 0 + + for polygon in hc.spine_ordered_polygons: + x_data = polygon.x_values + y_data = polygon.y_values + scatter = go.Scatter(x=x_data, y=y_data, + mode='lines', name='{}'.format(polygon_number), + opacity=0.8, marker_color='green') + + spine_x_values = [x[0] for x in polygon.spine] + spine_y_values = [x[1] for x in polygon.spine] + scatter_spine = go.Scatter(x=spine_x_values, y=spine_y_values, + mode='lines', name='{} Spine'.format(polygon_number), + opacity=0.8, marker_color='red') + row = math.ceil((counter + 1) / cols) + col = (counter % cols) + 1 + fig.add_trace(scatter, row=row, col=col) + fig.add_trace(scatter_spine, row=row, col=col) + counter += 1 + polygon_number += 1 + # plt_div = plot(fig, output_type='div') + plot_high_class_list.append(fig) + return plot_high_class_list + + def create_multiple_convex_polygons(number, max_ngon): polygon_list = [] for count in range(0, number): @@ -220,388 +280,107 @@ def create_convex_polygon( return polygon2 -def plot_polygons(polygon_list): - counter = 0 - polygon_number = len(polygon_list) - fig, ax = plt.subplots(polygon_number, figsize=(18, 16), dpi=80, facecolor='w', edgecolor='k') - plt.tight_layout(pad=10, w_pad=10, h_pad=3) - +def plot_polygons(polygon_list,render=True, plot_width=250, plot_height=250): + plots = [] for polygon in polygon_list: - x = polygon.x_values - y = polygon.y_values - for x_text, y_text in polygon.shell: - ax[counter].text(x_text, y_text, '({}, {})'.format(int(x_text * 10) / 10, int(y_text * 10) / 10), ) - ax[counter].plot(x, y) - ax[counter].scatter(x, y, s=60) - ax[counter].set_title('A single plot') - x1 = [x[0] for x in polygon.spine] - y1 = [x[1] for x in polygon.spine] - - ax[counter].plot(x1, y1) - mplcursors.cursor() - counter += 1 - - -def pack_polygons(polygons): - list_hc = height_classes(polygons) - list_containers = building_containers(list_hc) - list_mini_containers = pack_mini_containers(list_containers) - end_container = End_Container(list_mini_containers, True) - return end_container - - -class Container(object): - def __init__(self, hc): - self.hc = hc - self.hc_copy = deepcopy(hc) - self.y_boundary = hc.max_border - self.x_boundary = 0 - self.sigma = [] - self.Tree = avl_tree.AVLTree() - self.root = None - self.box_boundarys_x_values = [] - self.plots=[] - - def plot_container(self, sigma, r_l_distances=None, l_r_distances=None, min_distance=None): - fig, ax = plt.subplots(facecolor='w', edgecolor='k', figsize=(10, 8), dpi=90) - # x,y= self.exterior.xy - - for polygon in sigma: - x = [] - y = [] - for x_text, y_text in polygon.shell: - x.append(x_text) - y.append(y_text) - # ax.text(x_text, y_text, '({}, {})'.format(int(x_text*10)/10, int(y_text*10)/10)) - # print(x,y) - ax.plot(x, y) - ax.scatter(x, y, s=60) - x1 = [x[0] for x in polygon.spine] - y1 = [x[1] for x in polygon.spine] - ax.plot(x1, y1, linewidth=2, color='black') - if self.box_boundarys_x_values != []: - for boundary in self.box_boundarys_x_values: - ax.plot([self.box_boundarys_x_values, self.box_boundarys_x_values], [0, self.y_boundary], - linestyle='--') - ax.set_title('hc_{} Container -> Mini Containers'.format(self.hc.i)) - else: - ax.set_title('hc_{} Container'.format(self.hc.i)) - if r_l_distances != None: - for tuple in r_l_distances: - x = tuple[0][0] - corresponding_point_x = x - tuple[1] - y = tuple[0][1] - text_point_x, text_point_y = (x - tuple[1] / 2, y) - distance = int(tuple[1]) - ax.text(text_point_x, text_point_y, '{}'.format(distance)) - if distance == int(min_distance): - ax.plot([tuple[0][0], corresponding_point_x], [y, y], linestyle='--', marker='<', color='green') - else: - ax.plot([tuple[0][0], corresponding_point_x], [y, y], linestyle='--', marker='<', color='red') - if l_r_distances != None: - for tuple in l_r_distances: - x = tuple[0][0] - corresponding_point_x = x + tuple[1] - y = tuple[0][1] - text_point_x, text_point_y = (x + tuple[1] / 2, y) - distance = int(tuple[1]) - ax.text(text_point_x, text_point_y, '{}'.format(distance)) - if distance == int(min_distance): - ax.plot([tuple[0][0], corresponding_point_x], [y, y], linestyle='--', marker='>', color='green') - else: - ax.plot([tuple[0][0], corresponding_point_x], [y, y], linestyle='--', marker='>', color='blue') - mplcursors.cursor() - ax.plot([0, self.x_boundary, self.x_boundary, 0, 0], [0, 0, self.y_boundary, self.y_boundary, 0]) - return mpld3.fig_to_html(fig) - - def slope3(self, spine, point1): - # y = m*x+n - # (y-n)/m=x - # n = y-m*x - if spine[0][0] == spine[1][0]: # Sonderfall für eine senkrechte - distance = math.sqrt((spine[0][0] - point1[ - 0]) ** 2) # da der spine eine senkrechte ist -> der eintreffpunkt hat den gleichen y wert wie der punkt, in der Abstand formel fällt dieser wert weg - elif spine[0][1] == point1[1]: - distance = math.sqrt((spine[0][0] - point1[0]) ** 2 + (spine[0][1] - point1[1]) ** 2) - else: - slope = (spine[1][1] - spine[0][1]) / (spine[1][0] - spine[0][ - 0]) # m = y2-y1/x2-x1 Formel für die Geradensteigung mithilfe aus zwei verschiedenen Punkten der Geraden - # print("steigung", slope) - # print(point1[1]) - n = spine[0][1] - (slope * spine[0][0]) - # print("y-achsenabnschnitt",n) - point0_x = (point1[1] - n) / slope # x=(y-n)/m - # print("x-wert vom punkt",point0_x) - point0 = (point0_x, point1[1]) - distance = math.sqrt((point0[0] - point1[0]) ** 2 + (point0[1] - point1[1]) ** 2) - return distance - - def find_successor(self, vertex, array): - for counter, top_edge_point in enumerate(array): - if top_edge_point[1] >= vertex[1]: - return (array[counter - 1], top_edge_point) - - def packing_container(self, hc_polygons, plot=None): - tree = self.Tree - p = [] - l = [] - r = [] - b = [] - sigma_helper = [] # vi - for polygon in hc_polygons: - sigma_helper.append(polygon) - if self.sigma == []: - transform_x = -polygon.min_x - transform_y = -polygon.min_y - # polygon.plot_polygon("before translation") - polygon.translation(transform_x, transform_y) - - # polygon.plot_polygon("after translation") - polygon_vertices = len(polygon.right) - for count in range(0, polygon_vertices - 1): - vertice = polygon.right[count] - vertice_neighbour = polygon.right[count + 1] - self.root = self.Tree.insert_node(self.root, vertice[1], vertice, vertice_neighbour) - self.sigma.append(polygon) - self.x_boundary += polygon.width - t = deepcopy(self.sigma) # vi - # t.append(polygon) # vi - self.plots.append(self.plot_container(t)) # vi - else: - print("self.x boundary", self.x_boundary) - # if polygon.min_x < self.x_boundary: - transform_x = (self.x_boundary + abs(polygon.min_x)) + 10 - print("x_boundary:", self.x_boundary, "polygon min x abs", abs(polygon.min_x)) - print("transform x:", transform_x) - print("polygon min x", polygon.min_x) - # else: - # transform_x=0 - transform_y = -polygon.min_y - polygon.translation(transform_x, transform_y) - print(self.sigma) # vi - min_horizontal_distance = math.inf - print("tree/n", tree.printHelper(self.root, "", True)) - helper_0 = [] - for vertex in polygon.left: - vertex_y = vertex[1] - successor_vertex = self.Tree.find_edges(self.root, vertex_y, self.root) - corresponding_edge_points = (successor_vertex.vertice, successor_vertex.vertice_neighbour) - print("corresponding edge points", corresponding_edge_points, "for vertex", vertex) - distance = self.slope3(corresponding_edge_points, vertex) - if distance <= min_horizontal_distance: - min_horizontal_distance = distance - helper_0.append((vertex, distance)) - print("min_horizontal distance right to left", min_horizontal_distance) - print("helper_0", helper_0) - key = polygon.spine[1][1] - print("key to order tree array", key) - tree_array = self.Tree.preOrder_array((self.root), [], key) - print("tree_array in order ", tree_array) - helper_1 = [] - for vertex in tree_array: - print("vertex", vertex) - print("polygon left vertices", polygon.left) - successor = self.find_successor(vertex, polygon.left) - print(successor) - distance = self.slope3(successor, vertex) - print("distance", distance) - if distance <= min_horizontal_distance: - min_horizontal_distance = distance - self.root = self.Tree.delete_node(self.root, vertex[1]) # deleting vertices from B - helper_1.append((vertex, distance)) # ------- - # for count in range(0,polygon_vertices-1): - # vertice = polygon.translated_right[count] - # vertice_neighbour = polygon.translated_right[count+1] - # self.root = self.Tree.insert_node(self.root,vertice[1],vertice,vertice_neighbour) - print("min_horizontal distance left to right ", min_horizontal_distance) - self.plots.append(self.plot_container(sigma_helper, helper_0, helper_1, min_horizontal_distance)) # --------------------- - polygon.translation(-(min_horizontal_distance), 0) - print(polygon.right) - for counter, vertex in enumerate(polygon.right[0:-1]): - self.root = self.Tree.insert_node(self.root, vertex[1], vertex, (polygon.right[counter + 1])) - x_boundary = max([vertex[0] for vertex in polygon.right] + [self.x_boundary]) - self.x_boundary = x_boundary - self.sigma.append(polygon) - - if plot == True: - self.plots.append(self.plot_container(self.sigma)) - self.plots.append(self.plot_container(self.sigma)) - return self.sigma - - -def building_containers(height_classes, plot=None): - containers = [] - for height_class in height_classes: - container = Container(height_class) - if plot == True: - container.packing_container(container.hc.spine_ordered_polygons, plot=True) + plots.append(polygon.plot_polygon(render=False)) + grid = gridplot(plots, ncols=4, plot_width=plot_width, plot_height=plot_height) + if render: + return show(grid) + else: + return grid + + +def plotly_plot_polygons(polygons): + plot_polygon_list = [] + + polygon_count = len(polygons) + cols = 4 + rows = math.ceil(polygon_count / cols) + number = 0 + sub_plot_titles = [] + for polygon in polygons: + height = (int(polygon.height * 10)) / 10 + if polygon.slope == math.inf: + slope = math.inf else: - container.packing_container(container.hc.spine_ordered_polygons) - containers.append(container) - return containers - - -class Mini_Container(object): - def __init__(self, max_width, max_height): - self.current_right_boundary = 0 - self.max_width = max_width - self.height = max_height - self.c = 2.214 - self.polygons = [] - self.y_offset = 0 - - def plot_container(self, title=None): - fig, ax = plt.subplots(facecolor='w', edgecolor='k', figsize=(20, 12), dpi=90) - # x,y= self.exterior.xy - - for polygon in self.polygons: - x = [] - y = [] - for x_text, y_text in polygon.shell: - x.append(x_text) - y.append(y_text) - ax.text(x_text, y_text - (y_text * 0.1), - '({}, {})'.format(int(x_text * 10) / 10, int(y_text * 10) / 10), ) - # print(x,y) - ax.plot(x, y) - ax.scatter(x, y, s=60) - if title == None: - ax.set_title('Mini-Container') - else: - ax.set_title(title) - x1 = [x[0] for x in polygon.spine] - y1 = [x[1] for x in polygon.spine] - ax.plot(x1, y1, linewidth=2, color='black') - mplcursors.cursor() - ax.plot([0, self.max_width, self.max_width, 0, 0], - [0 + self.y_offset, 0 + self.y_offset, self.height + self.y_offset, self.height + self.y_offset, - 0 + self.y_offset]) - return plt.show() - - -def pack_mini_containers(container_array, plot=None): - mini_container_array = [] - for container in container_array: - copy_container = container # nochmal schauen ob der Schritt nötig ist bzw. ob nur ein Pointer kopiert wird statt eine deepcopy - box_width = 2.214 * copy_container.hc.w_max - box_width_helper = 0 - - box_counter = 1 - # print("container boundarys",copy_container.x_boundary) - while box_width_helper < copy_container.x_boundary: - container.box_boundarys_x_values.append(box_width_helper) - box_width_helper += box_width - if plot == True: - copy_container.plot_container(copy_container.sigma) - max_width_mini_container = box_width + copy_container.hc.w_max - while len(copy_container.sigma) > 0 and (box_width * box_counter) < (copy_container.x_boundary): - # print("max_with,y_boundary",max_width_mini_container,(copy_container.y_boundary)) - mini_container = Mini_Container(max_width_mini_container, (copy_container.y_boundary)) - translation_flag = True - while len(copy_container.sigma) > 0 and (copy_container.sigma[0].min_x) < (box_width * box_counter): - if translation_flag: - translation = copy_container.sigma[0].min_x - translation_flag = False - copy_container.sigma[0].translation(-translation, 0) - mini_container.polygons.append(copy_container.sigma.pop(0)) - # mini_container.plot_container() - mini_container.current_right_boundary = mini_container.polygons[-1].max_x - mini_container_array.append(mini_container) - if plot == True: - title = "Mini-Container {} (hc_{})".format(box_counter, container.hc.i) - mini_container.plot_container(title) - box_counter += 1 - if len(copy_container.sigma) > 0: - mini_container = Mini_Container(max_width_mini_container, copy_container.y_boundary) - translation_flag = True - while len(copy_container.sigma) > 0 and copy_container.sigma[0].min_x < box_width * box_counter: - if translation_flag: - translation = copy_container.sigma[0].min_x - translation_flag = False - copy_container.sigma[0].translation(-translation, 0) - mini_container.polygons.append(copy_container.sigma.pop(0)) - # mini_container.plot_container() - mini_container.current_right_boundary = mini_container.polygons[-1].max_x - mini_container_array.append(mini_container) - if plot == True: - title = "Mini-Container {} (hc_{})".format(box_counter, container.hc.i) - mini_container.plot_container(title) - return mini_container_array - - -class End_Container(object): - def __init__(self, mini_container_array, angle=0): - self.mini_containers = mini_container_array - self.pack_mini_containers() - self.container_area = self.container_area(self.mini_containers) - self.angle = angle - - def pack_mini_containers(self): - y_offset = self.mini_containers[0].height - for mini_container in self.mini_containers[1:]: - for polygon in mini_container.polygons: - polygon.translation(0, y_offset) - mini_container.y_offset = y_offset - y_offset += mini_container.height - return - - def container_area(self, mini_containers): - container_area = 0 - for mini_container in mini_containers: - container_area += mini_container.max_width * mini_container.height - return container_area - - def plot_container(self, title=None): - fig, ax = plt.subplots(facecolor='w', edgecolor='k', figsize=(20, 12), dpi=90) - # x,y= self.exterior.xy - y_offset = 0 - for mini_container in self.mini_containers: - for polygon in mini_container.polygons: - x = [] - y = [] - for x_text, y_text in polygon.shell: - x.append(x_text) - y.append(y_text) - ax.text(x_text, y_text - (y_text * 0.1), - '({}, {})'.format(int(x_text * 10) / 10, int((y_text) * 10) / 10), ) - # print(x,y) - ax.plot(x, y) - ax.scatter(x, y, s=60) - if title == None: - ax.set_title('End Container') - else: - ax.set_title(title) - x1 = [x[0] for x in polygon.spine] - y1 = [x[1] for x in polygon.spine] - ax.plot(x1, y1, linewidth=2, color='black') - ax.plot([0, mini_container.max_width, mini_container.max_width, 0, 0], - [0 + y_offset, 0 + y_offset, mini_container.height + y_offset, mini_container.height + y_offset, - 0 + y_offset]) - y_offset += mini_container.height - mplcursors.cursor() - return plt.show() - - - # polygons = create_multiple_convex_polygons(15,5) -# hc = height_classes(polygons) -# -# containers = building_containers(hc,True) #plot=True -# print(id(containers[0].hc.spine_ordered_polygons),id(containers[0].hc_copy.spine_ordered_polygons)) -# print(containers[0].hc.spine_ordered_polygons[0].plot_polygon(),"/n",containers[0].hc_copy.spine_ordered_polygons[0].plot_polygon()) -# mini_containers = pack_mini_containers(containers,True) #plot=True -# end_container = End_Container(mini_containers) -# end_container.plot_container() -#end_container.container_area - - - - - - - - - - - - - + slope = (int(polygon.slope * 10)) / 10 + sub_plot_titles.append('{} height:{} slope:{}'.format(number, height, slope)) + number += 1 + fig = make_subplots(rows=rows, cols=cols, subplot_titles=sub_plot_titles) + fig.update_layout(title="Convex Polygons") + counter = 0 + for polygon in polygons: + x_data = polygon.x_values + y_data = polygon.y_values + scatter = go.Scatter(x=x_data, y=y_data, + mode='lines', name='{}'.format(counter), + opacity=0.8, marker_color='green') + + spine_x_values = [x[0] for x in polygon.spine] + spine_y_values = [x[1] for x in polygon.spine] + scatter_spine = go.Scatter(x=spine_x_values, y=spine_y_values, + mode='lines', name='{} Spine'.format(counter), + opacity=0.8, marker_color='red') + row = math.ceil((counter + 1) / cols) + col = (counter % cols) + 1 + fig.add_trace(scatter, row=row, col=col) + fig.add_trace(scatter_spine, row=row, col=col) + counter += 1 + # plot_polygons_div = plot(fig, output_type='div') + + return fig + + +def high_class_plot(high_classes): + plot_high_class_list = [] + polygon_number = 0 + polygon_title_number = 0 + for hc in high_classes: + polygon_count = len(hc.polygons) + cols = 4 + rows = math.ceil(polygon_count / cols) + + # sub_plot_titles = ['{} height:{} slope:{}'.format(number, + # (int(hc.spine_ordered_polygons[number].height * 10)) / 10, + # (int(hc.spine_ordered_polygons[number].slope * 10)) / 10) + # for number in range(0, polygon_count)] + sub_plot_titles = [] + for polygon in hc.spine_ordered_polygons: + sub_plot_titles.append( + '{} height:{} slope:{}'.format(polygon_title_number, (int(polygon.height * 10)) / 10, + (int(polygon.slope * 10)) / 10)) + polygon_title_number += 1 + fig = make_subplots(rows=rows, cols=cols, subplot_titles=sub_plot_titles) + fig.update_layout(title="Highclass {} heights: {}-{}".format(hc.i, int(hc.min_border), int(hc.max_border))) + counter = 0 + + for polygon in hc.spine_ordered_polygons: + x_data = polygon.x_values + y_data = polygon.y_values + scatter = go.Scatter(x=x_data, y=y_data, + mode='lines', name='{}'.format(polygon_number), + opacity=0.8, marker_color='green') + + spine_x_values = [x[0] for x in polygon.spine] + spine_y_values = [x[1] for x in polygon.spine] + scatter_spine = go.Scatter(x=spine_x_values, y=spine_y_values, + mode='lines', name='{} Spine'.format(polygon_number), + opacity=0.8, marker_color='red') + row = math.ceil((counter + 1) / cols) + col = (counter % cols) + 1 + fig.add_trace(scatter, row=row, col=col) + fig.add_trace(scatter_spine, row=row, col=col) + counter += 1 + polygon_number += 1 + plt_div = plot(fig, output_type='div') + plot_high_class_list.append(plt_div) + return plot_high_class_list + + +# def pack_polygons(polygons): +# list_hc = height_classes(polygons) +# list_containers = building_containers(list_hc) +# list_mini_containers = pack_mini_containers(list_containers) +# end_container = End_Container(list_mini_containers, True) +# return end_container \ No newline at end of file diff --git a/mysite/plots/templates/plots/index.html b/mysite/plots/templates/plots/index.html index 29e16f30946399d881c01ebc83007cd5e16efd2a..0d3cd62d094751ca3dd786f667f9f7d7b4b9c89b 100644 --- a/mysite/plots/templates/plots/index.html +++ b/mysite/plots/templates/plots/index.html @@ -7,14 +7,7 @@ </head> <body> {% autoescape off %} -<h1>Convex Polygons</h1> - {{ polygons_plot }} -<h1>Highclasses</h1> -{% for high_class in high_classes %} - - {{ high_class }} - <hr> -{% endfor %} +{{polygons_plot}} {% endautoescape %} </body> diff --git a/mysite/plots/urls.py b/mysite/plots/urls.py index ad5e0be7cf4a52c9745b2a3f510b460286a0e3a5..27c6795589bc2fedb996e1e53c2804009914c3ac 100644 --- a/mysite/plots/urls.py +++ b/mysite/plots/urls.py @@ -4,5 +4,5 @@ from . import views urlpatterns = [ path('plot/', views.index, name='index'), - path('json/', views.get_json_view, name='index2'), + #path('json/', views.get_json_view, name='index2'), ] \ No newline at end of file diff --git a/mysite/plots/views.py b/mysite/plots/views.py index 7578121c48117eab10d28ca1503f4c1a99e69c70..594e4aecbf133e787e317a70459fe340ce4d4fab 100644 --- a/mysite/plots/views.py +++ b/mysite/plots/views.py @@ -4,8 +4,8 @@ from django.shortcuts import render from django.shortcuts import render from plotly.offline import plot import plots.polygon as poly +from bokeh.embed import file_html import matplotlib -matplotlib.use('Agg') from django.http import JsonResponse from plotly.graph_objs import Scatter import plotly.graph_objects as go @@ -16,58 +16,33 @@ import mpld3 from plotly.subplots import make_subplots import math from plots import plot_helpers - +from bokeh.plotting import figure, output_file, show +from bokeh.resources import CDN # def index(request): # return HttpResponse("Hello, world. You're at the polls index.") def index(request): - - - - - - #[(0.5, 0), (0, 1), (1, 0), (0.5, 0)] convex_polygons = poly.create_multiple_convex_polygons(10,3) - - polygons_plot = plot_helpers.polygon_plot(convex_polygons) - hc = poly.height_classes(convex_polygons) - plot_high_class_list =plot_helpers.high_class_plot(hc) - #containers = poly.building_containers(hc) - #plot_hc_container_list = plot_helpers.hc_container_plot(containers) - # plot_polygon_list = [] - # for polygon in convex_polygons: - # fig = go.Figure() - # x_data = polygon.x_values - # y_data = polygon.y_values - # scatter = go.Scatter(x=x_data, y=y_data, - # mode='lines', name='test', - # opacity=0.8, marker_color='green') - # fig.add_trace(scatter) - # plt_div = plot(fig, output_type='div') - # plot_polygon_list.append(plt_div) - # - # - # - # high_classes = poly.height_classes(convex_polygons) - - return render(request, 'plots/index.html', context={'polygons_plot': polygons_plot, - 'high_classes': plot_high_class_list,}) + polygons_plot=poly.plot_polygons(convex_polygons,False,500,500) + html_polygons = file_html(polygons_plot, CDN, "my plot") + return render(request, 'plots/index.html', context={'polygons_plot': html_polygons,}) + # 'high_classes': plot_high_class_list,}) # 'hc_container': hc_container}) -def get_json_view(request): - convex_polygons = poly.create_multiple_convex_polygons(10, 3) - hc = poly.height_classes(convex_polygons) - containers = poly.building_containers(hc) - - container_plots=[] - for container in containers: - container_plots.append(container.plots) - #g = convex_polygons[0].plot_polygon() - return render(request, 'plots/get.html', context={'containers': container_plots,}) \ No newline at end of file +# def get_json_view(request): +# convex_polygons = poly.create_multiple_convex_polygons(10, 3) +# hc = poly.height_classes(convex_polygons) +# containers = poly.building_containers(hc) +# +# container_plots=[] +# for container in containers: +# container_plots.append(container.plots) +# #g = convex_polygons[0].plot_polygon() +# return render(request, 'plots/get.html', context={'containers': container_plots,}) \ No newline at end of file diff --git a/mysite/requirements/base.txt b/mysite/requirements/base.txt index 8c98d6db3531471f39049943c83c5eee70d698d3..007b0b553f4f41427d861739d0dfaba358f3ff75 100644 --- a/mysite/requirements/base.txt +++ b/mysite/requirements/base.txt @@ -3,4 +3,5 @@ matplotlib==3.3.2 path==15.0.0 numpy==1.19.2 mplcursors==0.3 -mpld3 \ No newline at end of file +mpld3 +bokeh \ No newline at end of file