def get_echarts_instance(self, *args, **kwargs): device_data = models.Device.objects.values('device_type').annotate( count=Count('device_type')) device_types, counters = fetch(device_data, 'device_type', 'count') pie = Pie("设备分类", page_title='设备分类', width='100%') pie.add("设备分类", device_types, counters, is_label_show=True) battery_lifes = models.Device.objects.values('name', 'battery_life') names, lifes = fetch(battery_lifes, 'name', 'battery_life') bar = Bar('设备电量', page_title='设备电量', width='100%') bar.add("设备电量", names, lifes) page = Page.from_charts(pie, bar) return page
def test_fetch(self): names = fetch(DICT_LIST_DATA, 'name') self.assertListEqual(names, ['Alice', 'Bob', 'Charlie']) sexs = fetch(DICT_LIST_DATA, 'sex', default='male') self.assertListEqual(sexs, ['female', 'male', 'male']) names, ages = fetch(DICT_LIST_DATA, 'name', 'age') self.assertListEqual(names, ['Alice', 'Bob', 'Charlie']) self.assertListEqual(ages, [30, 56, 56]) names, ages, sexs = fetch(DICT_LIST_DATA, 'name', 'age', 'sex', defaults={'sex': 'male'}) self.assertListEqual(names, ['Alice', 'Bob', 'Charlie']) self.assertListEqual(ages, [30, 56, 56]) self.assertListEqual(sexs, ['female', 'male', 'male'])
def test_with_dict(self): """ Use dict.get(key) to pick item. """ names, ages = fetch(DICT_LIST_DATA, 'name', 'age', getter=lambda item, key: item.get(key)) self.assertListEqual(names, ['Alice', 'Bob', 'Charlie']) self.assertListEqual(ages, [30, 56, 56])
def test_custom_getter(self): my_getter = lambda item, key: item.get(key) data_list = [MockItem(1, 2, 3), MockItem(4, 5, 6), MockItem(7, 8, 9)] xs, ys, zs = fetch(data_list, 'x', 'y', 'z', getter=my_getter) self.assertListEqual([1, 4, 7], xs)