def get( chart_name=None, chart=None, no_cache=None, filters=None, from_date=None, to_date=None, timespan=None, time_interval=None, heatmap_year=None, ): if chart_name: chart = frappe.get_doc("Dashboard Chart", chart_name) else: chart = frappe._dict(frappe.parse_json(chart)) timespan = chart.timespan if chart.timespan == "Select Date Range": from_date = chart.from_date to_date = chart.to_date timegrain = chart.time_interval filters = frappe.parse_json(filters) or frappe.parse_json( chart.filters_json) account = filters.get("account") company = filters.get("company") if not account and chart_name: frappe.throw( _("Account is not set for the dashboard chart {0}").format( get_link_to_form("Dashboard Chart", chart_name))) if not frappe.db.exists("Account", account) and chart_name: frappe.throw( _("Account {0} does not exists in the dashboard chart {1}").format( account, get_link_to_form("Dashboard Chart", chart_name))) if not to_date: to_date = nowdate() if not from_date: if timegrain in ("Monthly", "Quarterly"): from_date = get_from_date_from_timespan(to_date, timespan) # fetch dates to plot dates = get_dates_from_timegrain(from_date, to_date, timegrain) # get all the entries for this account and its descendants gl_entries = get_gl_entries(account, get_period_ending(to_date, timegrain)) # compile balance values result = build_result(account, dates, gl_entries) return { "labels": [formatdate(r[0].strftime("%Y-%m-%d")) for r in result], "datasets": [{ "name": account, "values": [r[1] for r in result] }], }
def get_chart_config(chart, filters, timespan, timegrain, from_date, to_date): if not from_date: from_date = get_from_date_from_timespan(to_date, timespan) from_date = get_period_beginning(from_date, timegrain) if not to_date: to_date = datetime.datetime.now() # get conditions from filters conditions, values = frappe.db.build_conditions(filters) # query will return year, unit and aggregate value data = frappe.db.sql(''' select {unit} as _unit, SUM({value_field}), COUNT(*) from `tab{doctype}` where {conditions} and {datefield} BETWEEN '{from_date}' and '{to_date}' group by _unit order by _unit asc '''.format( unit = chart.based_on, datefield = chart.based_on, value_field = chart.value_based_on or '1', doctype = chart.document_type, conditions = conditions, from_date = from_date.strftime('%Y-%m-%d'), to_date = to_date ), values) # add missing data points for periods where there was no result result = get_result(data, timegrain, from_date, to_date, chart.chart_type) chart_config = { "labels": [get_period(r[0], timegrain) for r in result], "datasets": [{ "name": chart.name, "values": [r[1] for r in result] }] } return chart_config
def get_chart_config(chart, filters, timespan, timegrain, from_date, to_date): if not from_date: from_date = get_from_date_from_timespan(to_date, timespan) from_date = get_period_beginning(from_date, timegrain) if not to_date: to_date = now_datetime() doctype = chart.document_type datefield = chart.based_on aggregate_function = get_aggregate_function(chart.chart_type) value_field = chart.value_based_on or '1' from_date = from_date.strftime('%Y-%m-%d') to_date = to_date filters.append([doctype, datefield, '>=', from_date, False]) filters.append([doctype, datefield, '<=', to_date, False]) data = frappe.db.get_list(doctype, fields=[ '{} as _unit'.format(datefield), '{aggregate_function}({value_field})'.format( aggregate_function=aggregate_function, value_field=value_field), ], filters=filters, group_by='_unit', order_by='_unit asc', as_list=True, ignore_ifnull=True) result = get_result(data, timegrain, from_date, to_date) chart_config = { "labels": [get_period(r[0], timegrain) for r in result], "datasets": [{ "name": chart.name, "values": [r[1] for r in result] }] } return chart_config
def get_chart_config(chart, filters, timespan, timegrain, from_date, to_date): if not from_date: from_date = get_from_date_from_timespan(to_date, timespan) from_date = get_period_beginning(from_date, timegrain) if not to_date: to_date = now_datetime() doctype = chart.document_type datefield = chart.based_on value_field = chart.value_based_on or "1" from_date = from_date.strftime("%Y-%m-%d") to_date = to_date filters.append([doctype, datefield, ">=", from_date, False]) filters.append([doctype, datefield, "<=", to_date, False]) data = frappe.db.get_list( doctype, fields=[ "{} as _unit".format(datefield), "SUM({})".format(value_field), "COUNT(*)" ], filters=filters, group_by="_unit", order_by="_unit asc", as_list=True, ignore_ifnull=True, ) result = get_result(data, timegrain, from_date, to_date, chart.chart_type) chart_config = { "labels": [get_period(r[0], timegrain) for r in result], "datasets": [{ "name": chart.name, "values": [r[1] for r in result] }], } return chart_config