コード例 #1
0
    def test_create_coordinates_with_padding(self):
        def coordinate_callback(plot_geometries):
            line_geometries = plot_geometries['test']
            self.assertEqual(line_geometries[0].start.coordinates[X], 50)
            self.assertEqual(line_geometries[0].start.coordinates[Y], 250)
            self.assertEqual(line_geometries[0].end.coordinates[X], 400)
            self.assertEqual(line_geometries[0].end.coordinates[Y], 150)

            self.assertEqual(line_geometries[1].start.coordinates[X], 400)
            self.assertEqual(line_geometries[1].start.coordinates[Y], 150)
            self.assertEqual(line_geometries[1].end.coordinates[X], 750)
            self.assertEqual(line_geometries[1].end.coordinates[Y], 50)

        dc = DataCollection([[0, 0], [2, 1], [4, 3]], Property('first', 0),
                            Property('second', 1))
        g = Graph(
            Viewport(Y.size(300, inverse=True),
                     X.size(800),
                     padding=50,
                     attributes={
                         'height': Y,
                         'width': X
                     }))
        g.create.line(dc, 'test', X(dc.properties.first),
                      Y(dc.properties.second))

        g.svg2(after_create_coordinates=coordinate_callback)
コード例 #2
0
    def test_create_datatime_axis(self):
        def geometry_callback(plot_geometries):
            for g in plot_geometries['axis_X']:
                if isinstance(g, graph.Line):
                    print("Line")
                    print(g.start.datum)
                    print(g.end.datum)
                if isinstance(g, graph.Text):
                    print("Text")
                    print(g.point.datum)

        def coordinate_callback(plot_geometries):
            line_geometries = plot_geometries['axis_X']

            self.assertIsInstance(line_geometries[0], graph.Line)

            self.assertEqual(line_geometries[0].start.coordinates[Y], 250)
            self.assertEqual(line_geometries[0].start.coordinates[X], 50)
            self.assertEqual(line_geometries[0].end.coordinates[Y], 50)
            self.assertEqual(line_geometries[0].end.coordinates[X], 50)

            self.assertEqual(line_geometries[1].start.coordinates[Y], 250)
            self.assertEqual(line_geometries[1].start.coordinates[X], 50)
            self.assertEqual(line_geometries[1].end.coordinates[Y], 250)
            self.assertEqual(line_geometries[1].end.coordinates[X], 55)

            self.assertEqual(line_geometries[2].point.coordinates[Y], 250)
            self.assertEqual(line_geometries[2].point.coordinates[X], 55)

            self.assertEqual(line_geometries[13].start.coordinates[Y], 50)
            self.assertEqual(line_geometries[13].start.coordinates[X], 50)
            self.assertEqual(line_geometries[13].end.coordinates[Y], 50)
            self.assertEqual(line_geometries[13].end.coordinates[X], 55)

            self.assertEqual(line_geometries[14].point.coordinates[Y], 50)
            self.assertEqual(line_geometries[14].point.coordinates[X], 55)

        g = Graph(
            Viewport(Y.size(300, inverse=True),
                     X.size(800),
                     padding=50,
                     attributes={
                         'height': Y,
                         'width': X
                     }))

        datetime_list = [[datetime(2020, 1, day)] for day in range(15, 20)]
        datetime_collection = DataCollection(
            datetime_list,
            Property('default', 0, convert=lambda t: t.timestamp()))

        g.create.axis(X(datetime_collection.properties.default),
                      collection=datetime_collection,
                      options={'position': {
                          Y: 'bottom'
                      }})

        g.svg2(after_create_geometry=geometry_callback,
               after_create_coordinates=coordinate_callback)
コード例 #3
0
    def test_create_axis_coordinates(self):
        def coordinate_callback(plot_geometries):
            line_geometries = plot_geometries['axis_Y']

            self.assertIsInstance(line_geometries[0], graph.Line)

            self.assertEqual(line_geometries[0].start.coordinates[Y], 250)
            self.assertEqual(line_geometries[0].start.coordinates[X], 50)
            self.assertEqual(line_geometries[0].end.coordinates[Y], 50)
            self.assertEqual(line_geometries[0].end.coordinates[X], 50)

            self.assertEqual(line_geometries[1].start.coordinates[Y], 250)
            self.assertEqual(line_geometries[1].start.coordinates[X], 50)
            self.assertEqual(line_geometries[1].end.coordinates[Y], 250)
            self.assertEqual(line_geometries[1].end.coordinates[X], 55)

            self.assertEqual(line_geometries[2].point.coordinates[Y], 250)
            self.assertEqual(line_geometries[2].point.coordinates[X], 55)

            self.assertEqual(line_geometries[13].start.coordinates[Y], 50)
            self.assertEqual(line_geometries[13].start.coordinates[X], 50)
            self.assertEqual(line_geometries[13].end.coordinates[Y], 50)
            self.assertEqual(line_geometries[13].end.coordinates[X], 55)

            self.assertEqual(line_geometries[14].point.coordinates[Y], 50)
            self.assertEqual(line_geometries[14].point.coordinates[X], 55)

        dc = DataCollection([[0, 0], [2, 1], [4, 3]], Property('first', 0),
                            Property('second', 0))
        g = Graph(
            Viewport(Y.size(300, inverse=True),
                     X.size(800),
                     padding=50,
                     attributes={
                         'height': Y,
                         'width': X
                     }))
        #g.create.line(dc, 'test', X(dc.properties.first), Y(dc.properties.second))

        g.create.axis(Y(dc.properties.second),
                      collection=[0, 1, 2, 3, 4, 5, 6],
                      options={'position': {
                          X: 'left'
                      }})

        g.svg2(after_create_coordinates=coordinate_callback)
コード例 #4
0
    def test_create_lines(self):
        dc = DataCollection([[0, 0], [2, 1], [4, 3]], Property('first', 0),
                            Property('second', 0))
        g = Graph(
            Viewport(Y.size(300, inverse=True),
                     X.size(800),
                     padding=0,
                     attributes={
                         'height': Y,
                         'width': X
                     }))
        g.create.line(dc, 'test', X(dc.properties.first),
                      Y(dc.properties.second))

        svg_text = g.svg2()

        self.assertTrue('<line x1="0" y1="300" x2="400" y2="150"' in svg_text)
        self.assertTrue('<line x1="400" y1="150" x2="800" y2="0"' in svg_text)
コード例 #5
0
    def test_graph_plot_bounds(self):
        dc = DataCollection([[0, 0], [2, 1], [4, 8]], Property('first', 0),
                            Property('second', 1))
        g = Graph(
            Viewport(Y.size(300, inverse=True),
                     X.size(800),
                     padding=0,
                     attributes={
                         'height': Y,
                         'width': X
                     }))
        g.create.line(dc, 'test', X(dc.properties.first),
                      Y(dc.properties.second))
        g.create.axis(Y(dc.properties.second),
                      collection=[0, 1, 2, 3, 4, 5, 6],
                      options={'position': {
                          X: 'left'
                      }})

        self.assertEqual(g.plot_bounds(Y), (0, 8))
コード例 #6
0
    def test_creates_plot_geometry(self):
        def geometry_callback(plot_geometries):
            self.assertEqual(list(plot_geometries.keys()), ['test'])
            line_geometries = plot_geometries['test']
            #TODO: update line geometries to always use the gemomety class and not switch
            self.assertIsInstance(line_geometries[0], geometry.Line)
            self.assertEqual(line_geometries[0].start.datum, [0, 0])
            self.assertEqual(line_geometries[0].end.datum, [1, 2])

        dc = DataCollection([[0, 0], [1, 2]], Property('first', 0),
                            Property('second', 0))
        g = Graph(
            Viewport(Y.size(300, inverse=True),
                     X.size(800),
                     padding=30,
                     attributes={
                         'height': Y,
                         'width': X
                     }))
        g.create.line(dc, 'test', X(dc.properties.first),
                      Y(dc.properties.second))

        g.svg2(after_create_geometry=geometry_callback)
コード例 #7
0
ファイル: layout.py プロジェクト: JKiriS/PyGephi
 def endAlgo(self):
     from graph import Property
     self.graph.preview(Property.EDGE_CURVED, Property.newBool(True))
     self.graph.preview(Property.EDGE_RADIUS, 10.0)
コード例 #8
0
def graph():
    time_data = time_datas()

    #g.create.line(DataCollection(data, properties=[Property(name, callable), Property(name, int), 'property_name']), x='property_name', y='property_name')
    dc = DataCollection(
        time_data,
        Property('month',
                 0,
                 parse=lambda t: datetime.strptime(t, "%d-%m-%Y"),
                 convert=lambda t: t.timestamp()),
        Property('count', 1),
    )
    dl = date_list(inc_date(dc.meta.month.min, -timedelta(days=1)),
                   inc_date(dc.meta.month.max, timedelta(days=1)))

    dl_data_collection = DataCollection(
        dl, Property('default', 0, convert=lambda t: t.timestamp()))
    x, y = X(dc.properties.month), Y(dc.properties.count)

    g = Graph(
        Viewport(Y.size(300, inverse=True),
                 X.size(800),
                 padding=30,
                 attributes={
                     'height': Y,
                     'width': X
                 }))
    g.create.line(dc, 'counts', x, y)
    g.create.axis(x,
                  collection=dl,
                  tick_size=5,
                  tick_additional_offset=5,
                  tick_text=lambda d: d.strftime("%Y") if d.month == 1 else "",
                  tick_text_properties={
                      'text-anchor': "middle",
                      "dominant-baseline": 'hanging'
                  },
                  options={'position': {
                      Y: 'bottom'
                  }})
    g.create.axis(y,
                  collection=[0, 100, 200, 300, 400, 500, 600, 700],
                  tick_text_properties={'text-anchor': "end"},
                  options={'position': {
                      X: 'left'
                  }})

    #mdata = find_meta_data(time_data)
    time_data = to_timestamp(time_data, 0)

    padding = 30
    height = 300
    width = 800
    datamin = 0
    datamax = 700
    tic_length = 5
    datemin = min([d[0] for d in time_data])
    datemax = max([d[0] for d in time_data])

    datemin_date = (datetime.fromtimestamp(datemin).replace(
        day=1, hour=0, minute=0, second=0) - timedelta(days=1)).replace(day=1)
    datemax_date = (datetime.fromtimestamp(datemax).replace(
        day=1, hour=0, minute=0, second=0) + timedelta(days=1)).replace(day=1)
    datemin = datemin_date.timestamp()
    datemax = datemax_date.timestamp()

    scale_time = date_list(datemin_date, datemax_date)
    scale_timestamps = [st.timestamp() for st in scale_time]
    scale_text = [str(d.year) if d.month == 1 else "" for d in scale_time]

    scale_data_num = [0, 100, 200, 300, 400, 500, 600, 700]

    scale_data = [
        list(t) for t in zip([padding] * len(scale_data_num), scale_data_num)
    ]
    scale_data_start = scale(scale_data, 1, [datamin, datamax],
                             [height - padding, padding])
    scale_data_end = [[sde[0] - tic_length, sde[1]]
                      for sde in copy.copy(scale_data_start)]
    scale_data_text = zip([str(sdn) for sdn in scale_data_num], scale_data_end)
    scale_data_points = zip(scale_data_start, scale_data_end)
    scale_data_lines = [Line(start=s, end=e) for (s, e) in scale_data_points]

    scale_date_data = [
        list(t) for t in zip(scale_timestamps, [height - padding] *
                             len(scale_timestamps))
    ]
    scale_date_data_start = scale(scale_date_data, 0, [datemin, datemax],
                                  [padding, width - (padding * 2)])
    scale_date_data_end = [[sde[0], sde[1] + tic_length]
                           for sde in copy.copy(scale_date_data_start)]
    scale_date_points = zip(scale_date_data_start, scale_date_data_end)
    scale_date_lines = [Line(start=s, end=e) for (s, e) in scale_date_points]
    scale_date_text = zip(scale_text,
                          [[x, y + 10] for (x, y) in scale_date_data_end])

    time_data = scale(time_data, 0, [datemin, datemax],
                      [padding, width - (padding * 2)])
    time_data = scale(time_data, 1, [datamin, datamax],
                      [height - padding, padding])

    time_lines = []
    for tdi in range(0, len(time_data) - 1):
        time_lines.append(
            Line(start=(time_data[tdi][0], time_data[tdi][1]),
                 end=(time_data[tdi + 1][0], time_data[tdi + 1][1])))

    yaxis = Line(start=(padding, padding), end=(padding, height - padding))
    xaxis = Line(start=(padding, height - padding),
                 end=(width - padding, height - padding))

    return render_template('graph.html',
                           height=height,
                           width=width,
                           xaxis=xaxis,
                           yaxis=yaxis,
                           time_data=time_data,
                           time_lines=time_lines,
                           scale_data_lines=scale_data_lines,
                           scale_data_text=scale_data_text,
                           scale_date_lines=scale_date_lines,
                           scale_date_text=scale_date_text,
                           new_graph=g)