def _set_data(self, args): # data can be passed as a 2d array, a DataFrame or 2 or more Series if len(args) == 1: # check if data is a dataframe or 2d list data = args[0] if isinstance(data, pd.DataFrame): table = dataframe_to_gviz(data, datetime_cols=self.datetime_cols, allow_nulls=self.allow_nulls) self.json = table.ToJSon() self._dataframe = data elif (isinstance(data, list) and isinstance(data, list)): self._2d_array = data else: # Data can only be Series at this point try: df = pd.DataFrame(args[0]) for s in args[1:]: df[s.name] = s except: message = "Data must be passed as 2d array, a DataFrame, or 2 or more Series" raise JoogleChartsException(message) self._dataframe = df table = dataframe_to_gviz(df, datetime_cols=self.datetime_cols, allow_nulls=self.allow_nulls) self.json = table.ToJSon() if self._2d_array: self._num_cols = len(self._2d_array[0]) self._num_rows = len(self._2d_array) - 1 #minus one for header row else: self._num_cols = len(self._dataframe.columns) self._num_rows = len(self._dataframe)
def _set_render_properties(self, jooglechart): charts = jooglechart.charts view_cols = charts[0].view_cols # check if all charts have the same view cols if len(charts) > 1: charts = jooglechart.charts for chart in charts[1:]: if chart.view_cols != view_cols: message = "For SeriesFilter, all charts must have the same view cols" raise JoogleChartsException(message) series_indexes, series_names = jooglechart._get_viewable_series_indexes_and_names() # make data frame of series names to use for series filter DataTable df = pd.DataFrame({'columns': series_names}) # default selectedValues to all if not self._state.get('selectedValues'): self._state['selectedValues'] = series_names self._filter_table_json = dataframe_to_gviz(df).ToJSon() self._series_indexes = series_indexes self._num = get_joogle_object_counter() if self._label: self._name = self._label + FILTER_NAME_ADD_ON self._global_name = True else: self._name = "google_filter_" + str(self._num) self._div_id = self._name + "_div_id"
def __init__(self, type, *args, **kwargs): super(Filter, self).__init__(type) self._label = None self._json = None self._data_type = None self._data = kwargs.get('data') self._filter_column_index = None if 'data' in kwargs: self._data = kwargs['data'] if not (isinstance(self._data, pd.Series) or isinstance(self._data, pd.DataFrame)): raise JoogleChartsException("Filter data must be Series or DataFrame") if isinstance(self._data, pd.Series): if self._data.name == None: self._data.name = "choices" df = pd.DataFrame(self._data) else: df = self._data table = dataframe_to_gviz(df, allow_nulls=True) self._json = table.ToJSon()
def __init__(self, choices, show_child_filters=False): super(SuperCategoryFilter, self).__init__(None) # Filter.__init__(self, None) self.add_options(ui_label = "Options") self.add_options(filterColumnIndex = 0) self._filter_labels = [] self.show_child_filters = show_child_filters try: if isinstance(choices, pd.Series): pass else: choices = pd.Series(choices) except: message = "SuperCategoryFilter options must be a pandas Series or list" raise JoogleChartsException(message) choices.name = "options" df = pd.DataFrame(choices, columns=['options']) table = dataframe_to_gviz(df) self._json = table.ToJSon()