DJANGO FUSIONCHARTS:“ int64”类型的对象不可JSON序列化

时间:2018-06-27 18:53:03

标签: django fusioncharts

如果使用sqlite数据库中的数据,但该图有效,但使用csv文件却给了我这个错误,我不确定该怎么办

我在线上曾经研究过类似的问题,例如"TypeError: (Integer) is not JSON serializable" when serializing JSON in Python?,但是我能够找出解决方案……当我使用词典时,我仍然是django和Fusioncharts的初学者

我在下面附上了功能

谢谢

def chart2001(request):
data = pd.read_csv('20_Victims_of_rape.csv')
year = '2001'
dataSource = {}
dataSource['chart'] = { 
    "caption": "Click on each State for a Subgroup Analysis",
        "xAxisName": "Name of the State",
        "yAxisName": "Number of Reported crimes against women",
        "theme": "ocean",
        "paletteColors" : "#0075c2",
        "bgColor" : "#ffffff",
        "borderAlpha": "20",
        "canvasBorderAlpha": "0",
        "usePlotGradientColor": "0",
        "plotBorderAlpha": "10",
        "showXAxisLine": "1",
        "xAxisLineColor" : "#999999",
        "showValues" : "0",
        "divlineColor" : "#999999",
        "divLineIsDashed" : "1",
        "showAlternateHGridColor" : "0"

    }

dataSource['data'] = []
dataSource['linkeddata'] = []
d = data[(data['Year'] == 2001) & (data['Subgroup'] == 'Total Rape Victims')]
d = d.reset_index(drop=True)
# Iterate through the data in `Revenue` model and insert in to the `dataSource['data']` list.
for key in range(0,len(d)):
  data = {}
  data['label'] = d['Area_Name'][key]
  data['value'] = d['Rape_Cases_Reported'][key]
  data['link'] = 'newchart-json-'+ d['Area_Name'][key]
  dataSource['data'].append(data)
  # Create the linkData for cities drilldown    
  linkData = {}
  # Inititate the linkData for cities drilldown
  linkData['id'] = d['Area_Name'][key]
  linkedchart = {}
  linkedchart['chart'] = {
    "caption" : "Analysis of the subgroups of the Crime in - " + d['Area_Name'][key] ,
    "showValues": "0",
    "theme": "zune"
    }

  # Convert the data in the `City` model into a format that can be consumed by FusionCharts.     
  linkedchart['data'] = []
  # Filtering the data base on the Country Code
  doj = pd.read_csv('20_Victims_of_rape.csv')
  sub = doj[(doj['Year'] == 2001) & (doj['Area_Name'] == d['Area_Name'][key])]
  sub=sub.reset_index(drop=True)

  for key in range(0,len(sub)):
    arrDara = {}
    arrDara['label'] = sub['Subgroup'][key]
    arrDara['value'] = sub['Rape_Cases_Reported'][key]
    linkedchart['data'].append(arrDara)

  linkData['linkedchart'] = linkedchart
  dataSource['linkeddata'].append(linkData)

# Create an object for the Column 2D chart using the FusionCharts class constructor                      
column2D = FusionCharts("column2D", "ex1" , "1200", "600", "chart-1", "json", dataSource)
return render(request, 'women/2001.html', {'output': column2D.render()}, {'year':year}) 

0 个答案:

没有答案