Search code examples
pythonsqlalchemyflask-sqlalchemypygal

Pygal: Displaying information for each data point


I have been playing around with Pygal, creating some line graphs for a project I am working on. I currently have my y axis set to be the value recorded and the x axis being the date / time the test was conducted. However I would also like to link the serial number to each data point. At the moment when you hover on a data point you get the y value in bold and underneith that you get the date it was recorded.

Does anyone know if it is possible to link information to data points without them being an axis label?

For reference I currently have the serial numbers being added to the list: 'sn_list'.

        for row in line_graph_query:
            if str(row.date_time) >= start_date and str(row.date_time) <= end_date :
                min_values.append(float(row.minimum_value))
                max_values.append(float(row.maximum_value))
                recorded_values.append(float(row.recorded_value))
                sn_list.append(row.product_serial_number)
                date_list.append(row.date_time)
                number_of_records = number_of_records + 1
        print(min_values)
        print(max_values)
        print(recorded_values)
        distance_x_axis = math.floor(number_of_records/6)
        line_chart = pygal.Line(no_data_text='No result found', style=custom_style,x_labels_major_every=distance_x_axis, x_label_rotation=20, show_minor_x_labels=False )
        line_chart.title = 'Detailed Results of '+test_name+' tests of '+board_pn
        line_chart.x_labels = map(str,date_list)
        line_chart.add('Minimum', min_values)
        line_chart.add('Maximum', max_values)
        line_chart.add('Recorded', recorded_values)
        graph_render.append(line_chart.render_data_uri())
        graphs_to_render[test_name] = graph_render[-1]

Solution

  • You can set the tooltip to any text you like by providing your data as dicts (see the documentation here). Each value should be represented by a dict that has at least a value attribute, this is the same value that you were providing to the chart directly. There are then a number of other attributes you can set, amongst them label.

    You should be able to get the tooltips you want by changing the three lines that append data in your if structure:

    min_values.append({"value": float(row.minimum_value),
                       "label": row.product_serial_number})
    max_values.append({"value": float(row.maximum_value),
                       "label": row.product_serial_number})
    recorded_values.append({"value": float(row.recorded_value),
                            "label": row.product_serial_number})
    

    Unless you are using it somewhere else this also means that you have no need of the sn_list.