2016-08-15 22 views
-1

OpenpyxlのLineChart()機能を使用して、好きなようにグラフを描画する際に問題があります。openpyxlで線グラフを描画する - 軸/描画の問題

the official pageのドキュメントを使用していますが、This result in Excelがあります。

これは望ましい結果(ただ、私はそれをスタイルすることができ、データ・ポイントが正しい取得する必要があり、カラー/書式設定を無視する):私はにデータを再配置しようとした

enter image description here

縦彼らは文書で証明するのと同じ方法でmasterListリスト内のスライスが、私はグラフが実際

for i in masterList: 
    #print ("Appending ", i, "to the sheet") 
    sheet.append(i) 

セクション、そして下の行の間でデータをどのように使用するかを理解していない:

data = Reference(sheet, min_col = 4, min_row = 7, max_col = currentCell, max_row = 28) 

以下の全体機能。 version = "v1.9"、currentCell =データがある日付の数、sheetはワークブック内の現在アクティブなワークシートです。

def drawChart(self, sheet, currentCell, version): 
    print ("CurrentCell = ", currentCell) 
    ### Get the chart data 
    dateData, versionData, versionXABData = ([] for i in range(3)) #Make 3 lists 
    for i in range(currentCell): 
     temp = sheet.cell(row = 7, column = 4+i).value 
     if not temp: 
      temp = 0 
      dateData.append(temp) 
     else: dateData.append(temp) #Put the dates in a list 

    for i in range(currentCell): 
     temp = sheet.cell(row = 28, column = 4+i).value 
     if not temp: 
      temp = 0 
      versionData.append(temp) 
     else: versionData.append(temp) #Put the version Totals in another 

    for i in range(currentCell): 
     temp = sheet.cell(row = 27, column = 4+i).value 
     if not temp: 
      temp = 0 
      versionXABData.append(temp) 
     else: versionXABData.append(temp) #Put the version XAB bugs in another 

    print ("Dates are: ", dateData, '\n', "VersionData is: ",versionData, '\n', "Version XAB is: ", versionXABData, '\n') 

    masterList = [list() for i in range(currentCell)] #Make a list containing the total number of empty lists for each day we have data for 
    masterList[0].append("Date") 
    masterList[0].append("Total "+ version +" Bugs") 
    masterList[0].append("Total "+ version +" XAB Bugs") 
    print (masterList[0]) 


    for i in range(1, currentCell): 
     #print (" Length of dataData = ", len(dateData), '\n', "Length of versionData = ", len(versionData), '\n', "Length of versionXABData = ", len(versionXABData), '\n',"i = ", i) 
     masterList[i].append(dateData[i]) 
     masterList[i].append(versionData[i]) 
     masterList[i].append(versionXABData[i]) 

    for i in masterList: 
     #print ("Appending ", i, "to the sheet") 
     sheet.append(i) 

    chart1 = LineChart() 
    chart1.title = "DoT Bug Burndown" 
    chart1.style = 13 
    chart1.y_axis.title = "No of Bugs" 
    chart1.x_axis.title = "Date" 
    chart1.width = 30 
    chart1.height = 20 

    data = Reference(sheet, min_col = 4, min_row = 7, max_col = currentCell, max_row = 28) 
    chart1.add_data(data, titles_from_data=True) 
    sheet.add_chart(chart1, "K31") 
+1

*希望の方法でグラフを描くことができます* ** < - **実際に出力が期待されるものは誰でも推測する必要がありますそのように見えるように... –

+0

あなたは日付でプロットしたいように見えるので、 'PlotBy'引数を変更する必要があります(openpyxlで何が呼び出されているかはわかりません)。*スクリーンショットは*日付です。 –

+0

ありがとうございました! 希望の結果を表示するスクリーンショットを追加しました。 PlotBy引数を今調査します。 –

答えて

0

これは、新しいデータを読み込んだ後に新しいシートに書き込むことで解決しました。

元の意味で正しい軸方向を使用していなかったため、以下の改訂コードでは、masterListの内容をワークブックの新しいシート、データ列(行ではなく)に追加しています。サポート文書のLineChartの例に示されているとおりです。

def drawChart(self, sheet, sheet2, currentCell, version): 
    print ("CurrentCell = ", currentCell) 
    ### Get the chart data 
    dateData, versionData, versionXABData = ([] for i in range(3)) #Make 3 lists 
    for i in range(currentCell): 
     temp1 = sheet.cell(row = 7, column = 4+i).value 
     temp2 = str(temp1) 
     temp3 = temp2[:10] 
     if not temp1: 
      temp2 = 0 
      dateData.append(temp3) 
     else: dateData.append(temp3) #Put the dates in a list 

    for i in range(currentCell): 
     temp = sheet.cell(row = 28, column = 4+i).value 
     if not temp: 
      temp = 0 
      versionData.append(temp) 
     else: versionData.append(temp) #Put the version Totals in another 

    for i in range(currentCell): 
     temp = sheet.cell(row = 27, column = 4+i).value 
     if not temp: 
      temp = 0 
      versionXABData.append(temp) 
     else: versionXABData.append(temp) #Put the version XAB bugs in another 

    print ("Dates are: ", dateData, '\n', "VersionData is: ",versionData, '\n', "Version XAB is: ", versionXABData, '\n') 

    masterList = [list() for i in range(currentCell)] #Make a list containing the total number of empty lists for each day we have data for 
    masterList[0].append("Date") 
    masterList[0].append("Total "+ version +" Bugs") 
    masterList[0].append("Total "+ version +" XAB Bugs") 

    print ("MasterList = ", masterList[0]) 

    for i in range(1, currentCell): 
     #print (" Length of dataData = ", len(dateData), '\n', "Length of versionData = ", len(versionData), '\n', "Length of versionXABData = ", len(versionXABData), '\n',"i = ", i) 
     masterList[i].append(dateData[i]) 
     masterList[i].append(versionData[i]) 
     masterList[i].append(versionXABData[i]) 




    for i in masterList: 
     print ("Appending ", i, "to the sheet") 
     sheet2.append(i) 

    chart1 = LineChart() 
    chart1.title = version + " Bug Burndown" 
    chart1.style = 2 

    chart1.y_axis.title = "No of Bugs" 
    chart1.width = 30 
    chart1.height = 20 




    data = Reference(sheet2, min_col=2, min_row=1, max_col=3, max_row=36) 
    chart1.add_data(data, titles_from_data=True) 

    #s = Series(y, xvalues = x) 

    dates = Reference(sheet2, min_col=1, min_row=2, max_row=36) 
    chart1.set_categories(dates) 

    chart1.legend.position = 'b' 

    s1 = chart1.series[0] 
    s2 = chart1.series[1] 
    s1.graphicalProperties.line.width = 50000 
    s2.graphicalProperties.line.width = 50000