-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIndex.razor
More file actions
102 lines (87 loc) · 4.09 KB
/
Copy pathIndex.razor
File metadata and controls
102 lines (87 loc) · 4.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
@page "/"
@rendermode InteractiveServer
@inject Services.PivotTableExportService ExportService
@inject IJSRuntime JS
<PageTitle>Pivot Table</PageTitle>
<h1>Pivot Table</h1>
<DxButton Text="Export to Excel" Click="OnExportClick" CssClass="export-btn" />
<DxPivotTable Data="@PivotTableData" @ref="pivotTable" CssClass="h-auto">
<Fields>
<DxPivotTableField Area="PivotTableArea.Row"
Field="@nameof(SaleInfo.Region)"
SortOrder="PivotTableSortOrder.Descending" />
<DxPivotTableField Area="PivotTableArea.Row"
Field="@nameof(SaleInfo.Country)" />
<DxPivotTableField Area="PivotTableArea.Column"
Field="@nameof(SaleInfo.Date)"
GroupInterval="PivotTableGroupInterval.DateYear"
Caption="Year" />
<DxPivotTableField Area="PivotTableArea.Column"
Field="@nameof(SaleInfo.Date)"
GroupInterval="PivotTableGroupInterval.DateQuarter"
Caption="Quarter">
<ValueTemplate>
<span>@($"Q{context.Text}")</span>
</ValueTemplate>
</DxPivotTableField>
<DxPivotTableField Area="PivotTableArea.Data"
Field="@nameof(SaleInfo.Amount)"
SummaryType="PivotTableSummaryType.Sum"
CellFormat="C0" />
<DxPivotTableField Area="PivotTableArea.Data"
Field="@nameof(SaleInfo.OrderId)"
SummaryType="PivotTableSummaryType.Count"
Caption="Count" />
</Fields>
</DxPivotTable>
@code {
IPivotTable pivotTable { get; set; } = null!;
SaleInfo[]? PivotTableData;
protected override void OnInitialized() {
var years = new[] { DateTime.Now.Year - 3, DateTime.Now.Year - 2, DateTime.Now.Year - 1 };
var quarters = new[] { (1, 3), (4, 6), (7, 9), (10, 12) };
int id = 1;
PivotTableData = Regions
.SelectMany(region => region.Value
.SelectMany(country => years
.SelectMany(year => quarters
.Select(q => new SaleInfo(
id++,
region.Key,
country,
Random.Shared.Next(500, 10000),
new DateTime(year, Random.Shared.Next(q.Item1, q.Item2 + 1), Random.Shared.Next(1, 28))
)))))
.ToArray();
}
async Task OnExportClick() {
if (PivotTableData is null)
return;
var layout = pivotTable.SaveLayout();
var exportConfig = new Services.PivotTableExportConfig {
DataFieldConfigs = new() {
["OrderId"] = new Services.DataFieldConfig {
SummaryFunction = DevExpress.Spreadsheet.PivotDataConsolidationFunction.Count,
Caption = "Count"
}
},
ColumnFieldGroupings = new() {
["Date"] = new Services.ColumnFieldGroupingConfig {
GroupBy = DevExpress.Spreadsheet.PivotFieldGroupByType.Years | DevExpress.Spreadsheet.PivotFieldGroupByType.Quarters
}
}
};
var fileBytes = ExportService.ExportToExcel(PivotTableData, layout, exportConfig);
using var stream = new MemoryStream(fileBytes);
using var streamRef = new DotNetStreamReference(stream);
await JS.InvokeVoidAsync("downloadFileFromStream", "PivotTableExport.xlsx", streamRef);
}
public static readonly Dictionary<string, string[]> Regions = new() {
["Africa"] = ["South Africa", "Egypt"],
["Asia"] = ["UAE", "Japan", "India"],
["Europe"] = ["United Kingdom", "Germany", "Spain"],
["North America"] = ["USA", "Canada", "Mexico"],
["South America"] = ["Brazil", "Argentina", "Chile"]
};
public record SaleInfo(int OrderId, string Region, string Country, int Amount, DateTime Date);
}