import os
import json
import pandas as pd
from mdvtools.mdvproject import MDVProject
from mdvtools.charts.histogram_plot import HistogramPlot
[docs]
def create_histogram_plot(title, param, bin_number, display_min, display_max, size, position, x_axis_settings, y_axis_settings):
"""Create and configure a HistogramPlot instance with the given parameters."""
plot = HistogramPlot(
title=title,
param=param, # When the variable is named as "param", it can only take one data field. If it was "params" it would take more than one.
bin_number=bin_number,
display_min=display_min,
display_max=display_max,
size=size,
position=position
)
plot.set_x_axis(**x_axis_settings) # x-axis settings
plot.set_y_axis(**y_axis_settings) # y-axis settings
return plot
[docs]
def load_data(path):
"""Load data from the specified CSV file."""
return pd.read_csv(path, low_memory=False)
[docs]
def convert_plot_to_json(plot):
"""Convert plot data to JSON format."""
return json.loads(json.dumps(plot.plot_data, indent=2).replace("\\", ""))
[docs]
def main():
"""Main function to create the project and serve it."""
# Constants
project_path = os.path.expanduser('~/mdv/project')
data_path = "path_to_data"
view_name = "default"
# Create project
project = MDVProject(project_path, delete_existing=True)
# Load data
data_frame = load_data(data_path)
# Add datasource
project.add_datasource(data_path, data_frame)
# HistogramPlot parameters
title = "Number of cells"
param = "n_cells" # When the variable is named as "param", it can only take one data field. If it was "params" it would take more than one.
bin_number = 50
display_min = 0
display_max = 3000.035
size = [792, 472]
position = [10, 10]
x_axis_settings = {
'size': 30,
'label': "number of cells",
'textsize': 13,
'tickfont': 10
}
y_axis_settings = {
'size': 45,
'label': "frequency",
'textsize': 13,
'tickfont': 10,
'rotate_labels': False
}
# Create and configure plot
histogram_plot = create_histogram_plot(
title, param, bin_number, display_min, display_max, size, position, x_axis_settings, y_axis_settings
)
# Convert plot to JSON and set view
histogram_chart_json = convert_plot_to_json(histogram_plot)
histogram_view = {'initialCharts': {data_path: [histogram_chart_json]}}
project.set_view(view_name, histogram_view)
project.set_editable(True)
project.serve()
if __name__ == "__main__":
main()